1

How can i make a cached object re-cache it self with updated info when the cache has expired? I'm trying to prevent the next user who request the cache to have to deal with getting the data setting the cache then using it is there any background method/event i can tie the object to so that when it expires it just calls the method it self and self-caches.

2 Answers 2

2

You can use callback from Cache

System.Web.Caching.CacheItemRemovedCallback callback = 
    new System.Web.Caching.CacheItemRemovedCallback (OnRemove);
Cache.Insert("key",myFile,null, 
   System.Web.Caching.Cache.NoAbsoluteExpiration, 
   TimeSpan.Zero, 
   System.Web.Caching.CacheItemPriority.Default, callback);
 . . .
public static void OnRemove(string key, 
   object cacheItem, 
   System.Web.Caching.CacheItemRemovedReason reason)
   {
      // Logic
   }
Sign up to request clarification or add additional context in comments.

2 Comments

The issue i'm facing now with this implementation is that its stating HttpContext.Current.Cache is not available.
try using HttpContext.Current != null ? HttpContext.Current.Cache : HttpRuntime.Cache;
0

Pardon me, maybe I'm missing something. But the sounds like you are after to keep update the cached data. IMO, it's better to use CacheDependency instead of expiration in this case. Of course, you have to re-cache it on the next request.

1 Comment

The idea is to re-cache every xmin with out having the next request come in and have to manually kick off the re-caching.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.