ASP.net 创建自重新缓存对象?

发布于 2024-08-27 07:29:38 字数 114 浏览 3 评论 0原文

当缓存过期时,如何使缓存对象使用更新的信息重新缓存自身?我试图阻止下一个请求缓存的用户必须处理获取设置缓存的数据,然后使用它是否有任何后台方法/事件我可以将对象绑定到,以便当它过期时它只调用该方法它自我和自我缓存。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回心转意 2024-09-03 07:29:38

您可以使用缓存中的回调

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
   }

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
   }
岁吢 2024-09-03 07:29:38

请原谅我,也许我错过了一些东西。但听起来您想要不断更新缓存的数据。 IMO,在这种情况下最好使用 CacheDependency 而不是过期。当然,您必须在下一个请求时重新缓存它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文