Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

caching - What's the best method for forcing cache expiration in ASP.NET?

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

Can I:

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

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

  • 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...