Home > DeveloperSection > Forums > How to make basic CRUD on JSON file?
Elena Glibart
Elena Glibart

Total Post:35

Points:128
Posted on    November-03-2014 1:06 AM

 ASP.Net JSON  ASP.NET MVC 
Ratings:


 1 Reply(s)
 714  View(s)
Rate this:

I have a news site in ASP.NET MVC, where all the data comes from the homepage of the database. And this is generating a large traffic to my hosting server.

I have sessions like: Politics, Sports, World, among others, and each session is a different request made to the database. But each request only a small number of data is returned from the database, maximum 8 results for query. In total there are 21 requests made to the database.

I thought about making a data.json file and persist the session information in this file and show them in the frontend.

But as the file data.json and not a database, I have to do all the work to relocate the information when an action to delete or edit is performed.

Is there a better way to get to the point I want, considering that the site is already published?



Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    November-03-2014 3:43 AM

I would put a caching layer in between the site and the db so when you do one of these calls to the db it first checks the cache to see if the objects are there, if they are then it returns them from memory (super quick), if not it gets from db and populates the cache for next time. You must remember to clear the cache when the data in the DB gets changed though.

Cache Interface:

public interface ICacheProvider
{
    void Add(string key, object value);
    void Add(string key, object value, TimeSpan timeout);
    object Get(string key);
    object this[string key] { get; set; }
    bool Remove(string key);
    bool Contains(string key);
    void ClearAll();
}


Cache Implementation:

public class HttpCacheProvider : ICacheProvider

{

    /// <summary>

    /// The http cache

    /// </summary>

    private readonly Cache _cache;

 

    /// <summary>

    /// How often to expire the cache

    /// </summary>

    public int CacheExpiryMinutes { get; set; }

 

    public HttpCacheProvider()

        : this(HttpContext.Current.Cache, 60)

    {

    }

 

    public HttpCacheProvider(int cacheExpiryMinutes)

        : this(HttpContext.Current.Cache, cacheExpiryMinutes)

    {

    }

 

    public HttpCacheProvider(Cache cache, int cacheExpiryMinutes)

    {

        _cache = cache;

        CacheExpiryMinutes = cacheExpiryMinutes;

    }

 

    public void Add(string key, object value)

    {

        if (_cache[key] == null)

            _cache.Insert(key, value, null, DateTime.Now.AddMinutes(CacheExpiryMinutes), Cache.NoSlidingExpiration);

    }

 

    public void Add(string key, object value, TimeSpan timeout)

    {

        if (_cache[key] == null)

            _cache.Insert(key, value, null, DateTime.Now.Add(timeout), Cache.NoSlidingExpiration);

    }

 

    public object Get(string key)

    {

        return _cache[key];

    }

 

    public bool Contains(string key)

    {

        return (this[key] != null);

    }

 

    public object this[string key]

    {

        get { return _cache[key]; }

        set { _cache[key] = value; }

    }

 

    public bool Remove(string key)

    {

        return _cache.Remove(key) != null;

    }

 

    public void ClearAll()

    {

        var enumerator = _cache.GetEnumerator();

        while (enumerator.MoveNext())

        {

            _cache.Remove(enumerator.Key.ToString());

        }

    }

}


Usage:

private ICacheProvider _cacheProvider;

public IEnumerable<Something> GetData(int param1, int param2)
{        
    var cacheKey = string.Format("{0}-{1}", param1, param2);

        if (!_cacheProvider.Contains(cacheKey))
        {
            try
            {
                // get data from db

                // add that data to cahce
                _cacheProvider.Add(cacheKey, nodes);
            }
            catch (Exception ex)
            {
                // blah, logging
            }
        }

        return _cacheProvider[cacheKey] as IEnumerable<Something>;
    }

Don't want to miss updates? Please click the below button!

Follow MindStick