How to programatically clear ASP.NET cache?
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?
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:
- Programmatic Output Cache Entry Removal
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.
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();
}
精彩评论