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 :(

7
  • 1
    On which level do you want to add some cache layer? On controllers? Commented Mar 26, 2018 at 11:58
  • yes..on controller Commented Mar 26, 2018 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? Commented Mar 26, 2018 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 Commented Mar 26, 2018 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 - msdn.microsoft.com/en-us/library/…). 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. Commented Mar 26, 2018 at 12:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I have a list of countries..how to set cacheobject or cachekey?
cacheObject = <your list of countries>; . You can also make this a generic method and return (T)cacheObject if you don't want to cast in the caller.
and cachekey? its a type of string.
Yes, cachekey is how you will refer to the object in the cache. Be sure to use a logical and unique value because subtle bugs can occur if you re-use keys for different cache objects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.