Asp. NET output caching has many examples on the Internet, here is how to remove the cache in the background management.
1. Based on page caching
For pages: Default.aspx if the top of the page is added:
<%@ OutputCache duration= "No" varybyparam= "None"%>
It is easy to remove in the background management:
System.Web.HttpResponse.RemoveOutputCacheItem (Page.resolveurl ("default.aspx"));
2. Based on control
For control Webusercontrol.ascx If you add a
<%@ OutputCache duration= "a" varybyparam= "None" shared= "true"%>
In the background management to achieve a bit of trouble, in the blog Park Bo asked friends to answer, Charles provides a solution.
Implemented as follows:
(1) Add the VaryByCustom entry, the value is Cashgroupclass.
<%@ OutputCache duration= varybyparam= "None" shared= "true" varybycustom= "Cashgroupclass"%>
(2) Rewrite 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) in the background management of the removal page add the following code:
Cache Objcache = Httpruntime.cache;
if (objcache["Cashgroupclass"]!= null)
{
Objcache.remove ("Cashgroupclass");
}
Of course, you can also use this method to implement cache updates for controls. Yes, Charles posted in the code to use the Datacache class, is a write their own class, you can refer to the Datacache, but inside the overload parameter is not. Then 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 thank my friends for their help.
Reference: (1): Caching application pages and data (i)
(2): asp.net cache
(3): Where the getvarybycustomstring function in Global.asax.cs is invoked
(4): Datacache