使用Cache缓存Page页面 (转)

使用Cache:
<%@ OutputCache Duration="15" VaryByPara="None" %>
页面使用Cache,Cache的有效期为15 seconds

使用代码表示,放在Page_Load里面:
//
//Cache Manager可以控制这个页面
//
Response.Cache.SetCacheability(HttpCacheability.Public);

//
//告诉Cache Manager,页面的有效期为当前时间+15秒
//
Response.Cache.SetExpires(DateTime.Now.AddSeconds(15));


当使用@OutputCache时,Asp.Net 调用页面对象的InitOutPutCache(),将Cache转换为对应的HttpCachePolicy()。

所有对此页面的请求,都是访问Cache中的信息,直到Cache的生存周期结束。

对于页面的Cache的使用范围和存储位置:
Cache的使用范围可以使用Location属性,或者HttpResponse.Cache对象来设置。
<%@ OutputCache Duration="15" Location="Client" VaryByPara="None" %>
或者
Response.Cache.SetExpires(DateTime.Now.AddSeconds(15));
Response.Cache.SetCacheability(HttpCacheabiligy.Private);



默认状况下,Cache可以存放在任意位置,如:用户的浏览器,响应的服务器,或者代理服务器
<%@ OutputCache Duration="15" Location="Any" VaryByPara="None" %>
等效于:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(15));
Response.Cache.SetCacheability(HttpCacheability.Public);



如果禁止Cache,那么可以:
<%@ OutputCache Location="None" VaryByPara="None" %>
等效于:
Response.Cache.SetCacheability(HttpCacheability.NoCache);



使Cache存在于响应的服务器:
<%@ OutputCache Duration="15" Location="Server" VaryByPara="None" %>
等效于:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(15));
Response.Cache.SetCacheability(HttpCacheability.Server);



使Cache存在于Client:
<%@ OutputCache Duration="15" Location="Any" VaryByParam="Client" %>
等效于:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(15));
Response.Cache.SetCacheability(HttpCacheability.Private);



如果使用了集群,那么对于Cache的控制,应该由代理服务器来完成:
<%@ OutputCache Duration="15" Localtion="Location" VaryByPara="Client" %>
等效于:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(15));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetNoServerCaching();


注意:使用Location属性,不能保证任何页面的用户控件都使用定义的Location位置,同样:如果禁止使用Cache,也不能保证页面的用户控件都禁用了Cache。


http://support.microsoft.com/kb/323290
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值