在 ASP.NET 中强制缓存过期的最佳方法是什么? [英] What's the best method for forcing cache expiration in ASP.NET?

查看:22
本文介绍了在 ASP.NET 中强制缓存过期的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我可以:

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

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

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

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

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

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

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

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?

推荐答案

有很多方法可以让这些缓存过期,比如page outputcache by

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);

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

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

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

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

所以这里让您的缓存监控变得容易.坦率地说,我自己从未使用过它,但您可以尝试一下,只是向您的应用程序添加一个 dll 的问题.

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.

这是您的缓存键视图,

' 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

这篇关于在 ASP.NET 中强制缓存过期的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆