第一个写法:
//假如cache["ProcessIntensiveReport"]=null,outputcache会没有效果的。
//所以一定要设cache["ProcessIntensiveReport"]的值
Response.AddCacheItemDependency("ProcessIntensiveReport");
// Set additional properties to enable caching.
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
Response.Write(System.DateTime.Now.ToString());
Cache.Insert("ProcessIntensiveReport", "canue", null, System.DateTime.Now.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration);
//好多时候我们想同时清空outputcache和datacache,可以用上面的方法。
使用outputcache Dependencies datacache那个object。当清空datacache时同时清空outputcache.
//第二个写法:
在页面中加入:
<%@ OutputCache Location ="ServerAndClient" VaryByParam ="*" Duration ="60" %>
后台代码是:
protected void Page_Load(object sender, EventArgs e)
{
Cache.Insert("ProcessIntensiveReport", "canue", null, System.DateTime.Now.AddMinutes(60), System.Web.Caching.Cache.NoSlidingExpiration);
Response.AddCacheItemDependency("ProcessIntensiveReport");
Response.Write(System.DateTime.Now.ToString());
}
第一次运行了。它就会被cache了。实现了outputcache Dependencies,它的key为ProcessIntensiveReport,
如果你想清空cache的话。你需要在另一个页面实现清除cache的行为:
另一个页面的代码:
protected void Button2_Click(object sender, EventArgs e)
{
Cache.Remove("ProcessIntensiveReport");
}