Cache用法之页面声明
<%@ outputCache
Duration="#ofseconds"
Location="Any|Client|Downstream|Server|None"
VaryByControl="ControlName"
VaryByCustom="browser|customstring"
VaryByHeader="headers"
VaryByParam="Parametername" %>
Duration="#ofseconds"
Location="Any|Client|Downstream|Server|None"
VaryByControl="ControlName"
VaryByCustom="browser|customstring"
VaryByHeader="headers"
VaryByParam="Parametername" %>
Cache用法之代码控制
HttpCachePolicy类是专门用来控件Cache的,可以用Response.Cahce来访问这个类的实例
Response.Cache.SetExpires(DateTime.Now.AddSeceonds(10));
Response.Cache.SetCacheability(HttpCacheablility.Public);
Response.Cache.SetValidUnitlExpires(true);
Response.Cache.SetCacheability(HttpCacheablility.Public);
Response.Cache.SetValidUnitlExpires(true);
-----------------------以上都是缓存页面的,下面是缓存数据的----------------------------
Cache类的生存周期等于应用程序的生命周期
三种用法
1:存:Cache["key"] = MyData;取:
MyData = Cache["key"];
if(MyData != null)
use(MyData);
此法存入Cache的数据生命周期等于应用程序生命周期,不支持清除、过期、依赖性等功能。
2:存:
Cache.Insert(
string key,
object value,
CacheDependency dependencies,//依赖,设置缓存有效的依赖性,比如设置和一个文件相关,文件一变,就失效
DateTime absoluteExpireation, //设置固定的过期时间
TimeSpan slidingExpiration, //设置最后一次访问后多长时间过期
CachePriority priority, //设置内存不足,缓存自动清除时,缓存的重要性,可不可以清除
CacheItemRemovedCallback onRemoveCallback // 设置在清除时引发的事件
)
Example:
Cache.Insert("Mydata",MyData,new Caching.CacheDependency(Server.MapPah("Mydata.XML")));//设置有效性和一个文件有关
Cache.Insert("Mydata",myData,null,DateTime.Now.AddDays(1),Cache.NoSlidingExpiratin);//两种过期时间设了其中一种,另一种要设为0,用NoAbsolute(Sliding)Expiration枚举
Cache.Insert("MyData",myData,null,Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(10));//不能过一年不能小于0
Cache.Insert("MyData",myData,null,Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(10),
Caching.CacheItemPriority.NotRemovable,null);
// AboveNormal|BelowNormal|Default|High|Low|Normal|NotRemovable
public void RemovedCallback(string key,object value,CacheItemRemovedReason reason)
{
if(reason == CacheItemRemovedReason.DependencyChanged)
Response.Write("文件变了,快去看看");
}
Cache.Insert("Mydata",MyData,new Caching.CacheDependency(Server.MapPah("Mydata.XML"),
DateTime.Now.AddDays(1),Cache.NoSlidingExpiration,CacheItemPriority.High,
new CacheItemRemovedCallback(this.RemovedCallback));
Cache.Insert("Mydata",myData,null,DateTime.Now.AddDays(1),Cache.NoSlidingExpiratin);//两种过期时间设了其中一种,另一种要设为0,用NoAbsolute(Sliding)Expiration枚举
Cache.Insert("MyData",myData,null,Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(10));//不能过一年不能小于0
Cache.Insert("MyData",myData,null,Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(10),
Caching.CacheItemPriority.NotRemovable,null);
// AboveNormal|BelowNormal|Default|High|Low|Normal|NotRemovable
public void RemovedCallback(string key,object value,CacheItemRemovedReason reason)
{
if(reason == CacheItemRemovedReason.DependencyChanged)
Response.Write("文件变了,快去看看");
}
Cache.Insert("Mydata",MyData,new Caching.CacheDependency(Server.MapPah("Mydata.XML"),
DateTime.Now.AddDays(1),Cache.NoSlidingExpiration,CacheItemPriority.High,
new CacheItemRemovedCallback(this.RemovedCallback));
清除就可以用Cache.Remove("key");方法
3:
Cache.Add方法,用法和Insert差不多,区别在于Add碰到该key原来有赋过值会失败,Insert则不会,而会替换原有值;Add会返回被缓存数据项,Insert不会