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

时间:2021-09-15 03:53:30

Suppose I have an ASP.NET application running across several web servers behind a load balancer:

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

Can I:

我可以吗:

  • Force OutputCache (Page and/or Control level) to expire globally?

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

  • Force Data Cache (i.e. Cache.Insert) to expire?

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

  • Monitor ASP.NET caching usage (keys, RAM, etc) from a central location?

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

One possible solution would be to have every usage of cache check a file dependency for changes. The file could be touched which would expire all cache. However, this requires developers to include the dependency in all their code. Is their a better solution?

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

3 个解决方案

#1


7  

There are many ways to make these caching expire, like page outputcache by

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

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)

Time-based dependency simply expires the item at a defined point in time.

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

Response.Cache.SetExpires(DateTime.Now.AddSeconds(360));
Response.Cache.SetCacheability(HttpCacheability.Private)
Response.Cache.SetSlidingExpiration(true);

Now when it comes to monitoring cache, unless there is an API on the cache to tell you, then there is no direct way.

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

You could of course enumerate the cache,key-value pairs and then calculate the size of each item stored. Doesnt sound easy right??

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

So here's to make your cache monitoring easy. Frankly saying i never used it myself, but you can give it a try, just the matter of adding a dll to your application.

因此,这样可以轻松实现缓存监控。坦率地说,我自己从未使用它,但你可以尝试一下,只需在你的应用程序中添加一个dll即可。

And here's something for your cache keys view,

这里有一些缓存键视图,

' 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

#2


0  

From Tek-Tips (read link for detailed explanation)

来自Tek-Tips(阅读链接了解详细说明)

Response.Expires = 15
Response.ExpiresAbsolute = Now() - 2
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "private"

#3


0  

http://msdn.microsoft.com/en-us/library/y18he7cw(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/y18he7cw(v=vs.100).aspx

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);

#1


7  

There are many ways to make these caching expire, like page outputcache by

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

Page.Response.Cache.SetCacheability(HttpCacheability.NoCache)

Time-based dependency simply expires the item at a defined point in time.

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

Response.Cache.SetExpires(DateTime.Now.AddSeconds(360));
Response.Cache.SetCacheability(HttpCacheability.Private)
Response.Cache.SetSlidingExpiration(true);

Now when it comes to monitoring cache, unless there is an API on the cache to tell you, then there is no direct way.

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

You could of course enumerate the cache,key-value pairs and then calculate the size of each item stored. Doesnt sound easy right??

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

So here's to make your cache monitoring easy. Frankly saying i never used it myself, but you can give it a try, just the matter of adding a dll to your application.

因此,这样可以轻松实现缓存监控。坦率地说,我自己从未使用它,但你可以尝试一下,只需在你的应用程序中添加一个dll即可。

And here's something for your cache keys view,

这里有一些缓存键视图,

' 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

#2


0  

From Tek-Tips (read link for detailed explanation)

来自Tek-Tips(阅读链接了解详细说明)

Response.Expires = 15
Response.ExpiresAbsolute = Now() - 2
Response.AddHeader "pragma","no-cache"
Response.AddHeader "cache-control","private"
Response.CacheControl = "private"

#3


0  

http://msdn.microsoft.com/en-us/library/y18he7cw(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/y18he7cw(v=vs.100).aspx

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);