0

I have a function in controller which returns a list of countries.I am trying to implement caching in it..so that list is fetched only when empty at server side.I found two ways:

  1. Using OutputCache

     [OutputCache(Duration = 86400, Location = OutputCacheLocation.Server,VaryByParam ="none")]
     public function getelement(){
     //return list of country;
     }
    

But I am not able to test it maybe because both client and server is myself in this.

  1. Using MemoryCache

     private static MemoryCache _cache = MemoryCache.Default;
    
    _cache.Add(cacheKey, obj, policy);//MemoryCache(String, NameValueCollection, Boolean)
    

I am not able to correctly implement 2nd one.

Any suggestions?

Update 1:

   ObjectCache cache = MemoryCache.Default;
            string cacheKey = "countrylist";
            var cacheObject = cache.Get(cacheKey);

            if (cacheObject == null)
            {
                cacheObject = getelement();//returns a list of string type
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(60);
                cache.Add(cacheKey, cacheObject, policy);

            }    

I am not able to use cacheObject or access the list which now is of object type.

I tried following solutions:

1. var mylist = (from p in cacheObject.Cast<string>(IQueryable) select p).ToList();


2. List<string> mylist = cacheObject.AsQueryable().Cast<string>().Select(values).ToList();
 values.AddRange(mylist);

But I am not able to create a list of string type :(

Bhawna Jain
  • 709
  • 10
  • 27
  • 1
    On which level do you want to add some cache layer? On controllers? – arekzyla Mar 26 '18 at 11:58
  • yes..on controller – Bhawna Jain Mar 26 '18 at 11:59
  • 1
    One advantage of `OutputCache` is the ability to do client-side (i.e. browser caching) - which means the browser doesn't need to ask for the resource from your server. Is that of use to you? – mjwills Mar 26 '18 at 12:08
  • I guess no..cause I am trying to reduce the first time pageload time..and that wont make any differnce..its better to save the list in server's cache – Bhawna Jain Mar 26 '18 at 12:09
  • 2
    `its better to save the list in server's cache` `OutputCache` can do **both** (client and server caching). Plus it can cache on any downstream server (e.g. proxy - https://msdn.microsoft.com/en-us/library/system.web.ui.outputcachelocation(v=vs.110).aspx). Plus if you later add a CDN you will get improved load times again. Honestly, if you are trying to cache the entire http request you should strongly consider `OutputCache`. That is what it is for. – mjwills Mar 26 '18 at 12:13
  • How many web servers are you running? Are you using a web farm or web garden? – mjwills Mar 26 '18 at 12:17
  • Possible duplicate of [Pros/Cons of different ASP.NET Caching Options](https://stackoverflow.com/questions/18937855/pros-cons-of-different-asp-net-caching-options) – mjwills Mar 26 '18 at 12:19

1 Answers1

1

MemoryCache.Default is already static, there is no need to store it again.

You can implement similar to this:

ObjectCache cache = MemoryCache.Default;
var cacheObject = cache.Get(cacheKey);

if (cacheObject == null)
{
    cacheObject = //do something to get it
    CacheItemPolicy policy = new CacheItemPolicy();
    policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(2);
    cache.Add(cacheKey, cacheObject, policy);
}
return cacheObject;

Update

To @mjwills point, if you have a web farm/load balanced site each server will hold it's own cache and they could be out of sync (for at least the duration of the expiration) between servers. You might be OK with this, or you might not. If not consider NCache, Redis, or similar product.

Crowcoder
  • 11,250
  • 3
  • 36
  • 45