asp.net 之高速缓存

一、输出高速缓存

页面顶部插入:

<%@ OutputCache Duration="60" VaryByParam="None" %>

<%@ OutputCache Duration="60" VaryByParam="id;page" %>

二、部分页面高速缓存 (UserControl)

<%@ OutputCache Duration="60" VaryByParam="*" Shared="true" %>

三、Post-Cache Substitution

1.调用新方法Response.WriteSubstitution,给它传送对替换方法回调的引用。

2.在页面上的指定位置添加一个<asp:Substitution>控件,把它的methodName属性设置为回调方法的名称

四、HttpCachePolicy和客户端高速缓存

<%@ Import NameSpace=”System.Web.HttpCachePolicy” %>

Response.Cache.SetExpires(DateTime.Now.AddSeconds(10))‘指定页面输出缓存下一个10秒到期

Response.Cache.SetCacheability(HttpCacheability.Public) ‘指定所有用户都有对缓存的访问权力

如果不希望进行页面缓存,可采用Response.Cache.SetSlidingExpiration方法,当其为True时,每次页面请求到达时,相当于页面过期时间到了,就要对页面输出重新刷新 。

Response.Cache.SetSlidingExpiration(True)‘当每次页面请求时,重置到期时间计数器,并且页面到期

eg:

void Page_Load(s As Object, E As EventArgs)

{

Response.Cache.SetExpires(DateTime.Now.addseconds(10));

Response.Cache.SetCacheability(Httpcacheability.Public);

lblTime.Text ="现在时间是:" & DateTime.Now.ToString();

}

五、编程高速缓存

Cache[""] = object;

六、同一Request中缓存

 HttpContext.Current.Items.Add(object objKey, object objVal);

 * 此缓存只在同一Request中共享。如页面A.ASPX.CS中生成的Item。当用Server.Trancfer("B.ASPx")转向时,可以在B中获取或使用A中生成的Item。相反,Redirect("")则不能。前者发生在服务器端,和A是同一Request

******** 缓存依赖Cache dependency ********