ASP.net Create Self re-caching objects?
-
23-09-2019 - |
Question
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.
Solution
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
}
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow