Clearing the page cache in ASP.NET

On my blog, I want to use the output cache to keep the cached version of the daily message for 10 minutes, and that’s fine ...

<%@OutputCache Duration="600" VaryByParam="*" %> 

However, if someone sends a comment, I want to clear the cache so that the page refreshes and the comment can be seen.

How to do this in ASP.Net C #?

+49
c # outputcache
Aug 14 '08 at 19:39
source share
8 answers

I found the answer I was looking for:

 HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx"); 
+45
Aug 14 '08 at 20:04
source share

The above is good if you know on which pages you want to clear the cache. In my instance (ASP.NET MVC), I referenced the same data from around. So when I did [save], I wanted to clear the cache site. This is what worked for me: http://aspalliance.com/668

This is done in the context of the OnActionExecuting filter. This could be easily done by overriding OnActionExecuting in BaseController or something like that.

 HttpContextBase httpContext = filterContext.HttpContext; httpContext.Response.AddCacheItemDependency("Pages"); 

Setup:

 protected void Application_Start() { HttpRuntime.Cache.Insert("Pages", DateTime.Now); } 

Small setting: I have an assistant that adds “flash messages” (error messages, success messages - “This item was saved successfully”, etc.). To avoid the appearance of a flash message on each subsequent GET, I had to invalidate after recording a flash message.

Clearing Cache:

 HttpRuntime.Cache.Insert("Pages", DateTime.Now); 

Hope this helps.

+38
May 20 '10 at 18:24
source share

Using Response.AddCacheItemDependency to clear all output files.

  public class Page : System.Web.UI.Page { protected override void OnLoad(EventArgs e) { try { string cacheKey = "cacheKey"; object cache = HttpContext.Current.Cache[cacheKey]; if (cache == null) { HttpContext.Current.Cache[cacheKey] = DateTime.UtcNow.ToString(); } Response.AddCacheItemDependency(cacheKey); } catch (Exception ex) { throw new SystemException(ex.Message); } base.OnLoad(e); } } // Clear All OutPutCache Method public void ClearAllOutPutCache() { string cacheKey = "cacheKey"; HttpContext.Cache.Remove(cacheKey); } 

It can also be used in ASP.NET MVC OutputCachedPage.

+5
Jan 6 '09 at 13:55
source share

In the main page load event, write the following:

 Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetNoStore(); 

and in the exit button:

 Session.Abandon(); Session.Clear(); 
+3
Jun 22 '11 at 12:24
source share

Hm. You can specify the VaryByCustom attribute for the OutputCache element. The value of this parameter is passed as a parameter of the GetVaryByCustomString method, which can be implemented in global.asax. The value returned by this method is used as an index in the cached items - if you return the number of comments on the page, for example, each time a comment is added, the new page will be cached.

The caveat is that this does not actually clear the cache. If a blog post gets heavy use of comments, your cache may explode in size using this method.

Alternatively, you can implement non-changing page bits (navigation, ads, actual blog post) as user controls and implement partial page caching for each of these user controls.

+1
Aug 14 '08 at 19:51
source share

If you change "*" only to the parameters to which the cache should change (PostID?), You can do something like this:

 //add dependency string key = "post.aspx?id=" + PostID.ToString(); Cache[key] = new object(); Response.AddCacheItemDependency(key); 

and when someone adds a comment ...

 Cache.Remove(key); 

I assume this will work even with VaryByParam *, since all requests will be bound to the same cache dependency.

+1
Aug 14 '08 at 19:55
source share

why not use sqlcachedependency in the posts table?

sqlcachedependency msdn

So you don't implement custom cache flush code and just update cache when content changes in db?

+1
Jan 02 '09 at 0:17
source share

HttpRuntime.Close() .. I try the whole method and this is the only thing that works for me

0
Jan 18 '11 at 18:55
source share



All Articles