0

In ASP.NET Webforms, I can use <%@ OutputCache Duration="3600" VaryByParam="none"%> to cache a web page. I have very static data that will be infrequently updated, but when it is updated the HTML on the web page will change. This data will be tied to a management system allowing maintainers to make appropriate edits.

I'd like to have a way to set the duration of the OutputCache to be very long. But I would also like to be able to clear this web page from the cache when the data is updated (preferably from the data editing page).

What is the standard way of accomplishing this in ASP.NET?

Peter Smith
  • 849
  • 2
  • 11
  • 28

3 Answers3

1
For Each de As DictionaryEntry In HttpContext.Current.Cache
   HttpContext.Current.Cache.Remove(DirectCast(de.Key, String))
Next

MSDN Cache.Remove

Edit: Here are further informations on how to remove a page from the OutputCache:

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @Oskar: the way is the same in C#, do you have [problems with converting](http://www.developerfusion.com/tools/convert/vb-to-csharp/)? `foreach (DictionaryEntry de in HttpContext.Current.Cache) { HttpContext.Current.Cache.Remove((string)de.Key); }` – Tim Schmelter Oct 28 '11 at 06:58
  • 3
    it was intended to be humorous. – Oskar Austegard Nov 11 '11 at 02:40
  • This code won´t work in IIS 7+, because it won´t be cached on HttpContext.Current.Cache since it uses kernel cache by default. Follow the link in the answer and use the HttpResponse.RemoveOutputCacheItem("vistual path to page here"); method. More info here http://stackoverflow.com/questions/565239/any-way-to-clear-flush-remove-outputcache – repeatdomiau Apr 25 '14 at 05:07
  • @Gabriel: good point, I've forgotten the kernel cache (which can be enable d also in IIS7). However, Haven't I already mentioned the Removeoutputcacheitem method above? Follow my link. – Tim Schmelter Apr 25 '14 at 05:35
0

I think the marked solution doesn't work on II7+. So here's another approach that I used for a very similar problem.

If your DB is SQL Server you could use SQLDependency, but if not the workaround would be to use a filedependency as follows.

The outputcache mechanism supports a filedependency that works beautifully. Everytime the dependency file is modified your page gets cleared automatically.

So, You can have your dataentrypage write a log/text file, upon every succesful database updates. Make the log file a dependency to the webpage in question.

So. In your webpage.aspx.cs

protected void Page_Load(object sender, EventArgs e) { string fileDependencyPath = Server.MapPath("~/dblog/dataupdate.txt"); Response.AddFileDependency(fileDependencyPath); // rest of the code }

In your dataentrypage.aspx.cs

       string path = "~/dblog/dataupdatelog.txt";
        if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
            File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
        }
        using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
        {
            w.WriteLine("\r\nLog Entry : ");
            w.WriteLine("{0}", DateTime.Now.ToString());
            string strlog = "Some additional info";
            w.WriteLine( strlog);
            w.WriteLine("__________________________");
            w.Flush();
            w.Close();

}

0

AFAIK, ASP.NET will detect whether or not the page has changed and, if so, send the newly generated output down the wire (which will then be cached as part of the process); then, when the page is next requested it is drawn from the cache - unless the page has changed yet again.

From MSDN:

When a cached page is requested by a user, ASP.NET determines whether the cached output is still valid based on the cache policy you have defined for the page. If the output is valid, the cached output is sent to the client and the page is not re-processed. ASP.NET allows you to run code during this validation check so that you can write custom logic to check whether the page is valid.

What you might want to look into though, for finer-grained control (should you need it), is implementing a handler which serves pages from the cache for you - doing it this way would allow you to implement logic to determine if a page has changed or not, and determine whether to serve up the cache; you could refer to the other answer to determine exactly how to manipulate the cache - however, I can't see this being needed at all.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129