There are already many examples of using the ASP. NET output cache. Here we will mainly introduce how to remove the cache from the background management.
1. Based on page Cache
For page: Default. aspx, if you add:
<% @ OutputCache Duration = "60" VaryByParam = "none" %>
In the background management, it is easy to remove:
System. Web. HttpResponse. RemoveOutputCacheItem (Page. ResolveUrl ("Default. aspx "));
2. control-based
If WebUserControl. ascx is added to the top
<% @ OutputCache Duration = "60" VaryByParam = "none" Shared = "true" %>
It is a little troublesome to implement it in the background management. In the blog garden, ask friends to answer questions. Charles provides a solution.
The implementation is as follows:
(1) Add a VaryByCustom entry with the value Cashgroupclass.
<% @ OutputCache Duration = "60" VaryByParam = "none" Shared = "true" VaryByCustom = "Cashgroupclass" %>
(2) override the GetVaryByCustomString method in Global. asax. The Code is as follows:
Code
Public override string GetVaryByCustomString (HttpContext context, string arg)
{
If (arg = "Cashgroupclass ")
{
Cache objCache = HttpRuntime. Cache;
Object _ flag = objCache ["Cashgroupclass"];
If (_ flag = null)
{
_ Flag = DateTime. Now. Ticks. ToString ();
ObjCache. Insert ("Cashgroupclass", _ flag );
}
Return _ flag. ToString ();
}
Return base. GetVaryByCustomString (context, arg );
}
(3) Add the following code on the remove page of the background management:
Cache objCache = HttpRuntime. Cache;
If (objCache ["Cashgroupclass"]! = Null)
{
ObjCache. Remove ("Cashgroupclass ");
}
You can also use this method to update the control cache. By the way, Charles's Code uses the DataCache class, which is a self-written class. You can refer to DataCache, but the overload parameters in it are not correct. Add one.
Code
Public static void SetCache (string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
HttpRuntime. Cache. Insert (CacheKey, objObject, null, absoluteExpiration, slidingExpiration );
}
Finally, I would like to thank my friends for their help.
Reference: (1) cache application pages and data (1)
(2): ASP. NET Cache
(3): Where is the GetVaryByCustomString function called in Global. asax. cs?
(4): DataCache