In my project, we use web services to get data from the DB and populate the data from web services to cache. I am using Absolute expiration , and the duration is set to 4 hours. So after 4 hours, cache expires and therefore the web service is called to re populate the data in the cache.
The problem happening now is :
Sometimes there is a problem establishing connection between web services and database and therefore the web services does not return data.
In this case the cache is populated with null values for all fields and up to 4 hours cache (up to the time it expires again) it is not re populated.
I am looking for 2 types of solutions: 1)we could renew the cache before it expires, then call the web service. If the web service gets data back, overwrite the cache. 2) the cache never expires and every so often we spawn a thread which updates the cache if we get data back.
But I am not sure how to implement the above solutions? Can somebody give me insight on technical part? Thanks for the help!
MY current code of populating cache :
ctx.Cache.Insert("cachename", cacheValue, null,
DateTime.Now.AddHours(Int32.Parse(siteViewModel.ApplicationSettings["CacheDurationHours"])), System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,
null
);
Instead of passing null, implement a CacheItemRemovedCallback as the last parameter to Cache.Insert(..). When retrieving the value to repopulate the cache, you can do a null check and use the old value for the time being.