在ASP.NET中强制缓存过期的最佳方法是什么?

fra*_*lic 10 asp.net caching outputcache

假设我有一个ASP.NET应用程序在负载均衡器后面的几个Web服务器上运行:

我可以吗:

  • 强制OutputCache(页面和/或控制级别)全局到期?

  • 强制数据缓存(即Cache.Insert)到期?

  • 从中央位置监控ASP.NET缓存使用情况(密钥,RAM等)?

一种可能的解决方案是使每次缓存使用检查文件依赖性以进行更改.可以触摸该文件,该文件将使所有缓存失效.但是,这要求开发人员在所有代码中包含依赖项.他们是更好的解决方案吗?

sim*_*rsh 8

有很多方法可以使这些缓存过期,比如页面输出缓存

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)
Run Code Online (Sandbox Code Playgroud)

基于时间的依赖性简单地在定义的时间点使项目到期.

Response.Cache.SetExpires(DateTime.Now.AddSeconds(360));
Response.Cache.SetCacheability(HttpCacheability.Private)
Response.Cache.SetSlidingExpiration(true);
Run Code Online (Sandbox Code Playgroud)

现在谈到监控缓存,除非缓存上有一个API告诉你,否则没有直接的方法.

您当然可以枚举缓存,键值对,然后计算存储的每个项目的大小.听起来不容易吗?

所以,这里是让你的超高速缓存监控容易.坦率地说,我自己从未使用它,但你可以尝试一下,只需在你的应用程序中添加一个dll即可.

这里有一些缓存键视图,

' display contents of the ASP.NET Cache
If Cache.Count > 0 Then    
  cc.Append("<b>Contents of the ASP.NET Cache (" _    
          & Cache.Count.ToString() & " items):</b><br />")    
  For Each item As Object In Cache    
    cc.Append("Key:'" & item.Key & "' Type:" _    
            & item.Value.GetType().ToString() & "<br />")    
  Next    
Else    
  cc.Append("<b>ASP.NET Cache is empty</b>")    
End If
Run Code Online (Sandbox Code Playgroud)