Asp.Net 缓存技术总结


缓存
缓存机制
页输出缓存:保存页处理输出,下次重用所保存的输出
应用程序缓存:允许缓存所生成的数据,如DataSet

㈠页输出缓存

1
、页输出缓存的几中形式
<%@   OutputCache   Duration= "60 "   VaryByParam= "None "   Location= "Any "%>
Location
指定在哪个地方缓存,Any任何地方都缓存。
60
秒以内看到的都是一样的了。
还可在配置文件里写,然后在页面调用配置文件的缓存名称。
用编程的方式:
Response.Canche.SetExpires(DateTime.Now.AddSeconds(3));
Response.Canche.SetCacheabiliy(HttpCacheability.Public);
Response.Canche.SetValidUntilExpires(true);
相当于:
Public   =>   Any
Private   =>   Client
NoCache   =>   None
Server   =>   Server
ServerAndPrivate   => ServerAndClient

2
、使用文件依赖项缓存页输出
产生背景:有时候,可能需要在文件发生更改时从输出缓存中移除某一项。就是说文件改了以后缓存立即失效。
string   filepath   =   Server.MapPath( "TextFile1.txt ");
Response.AddFileDependency(filepath);//
添加缓存依赖项
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntiExpires(true);

3
、缓存多个版本
使用请求的浏览器对页的各个版本进行缓存
<%@OutputCache   Duration= "10 "   VaryByParam= "None "   VaryByCustom= "browser "%>
使用参数对页的各个版本进行缓存
<%@OutputCache   Duration= "60 "   VaryByParam= "City "%>
这个调试可以在url后加QueryString
如:...url?City=shanghai
程序里得到这个上海然后再做其他的操作,这个时候如果参数传的还是shanghai它就不会在走到程序里了。

4
、动态更新缓存页的部分,有三种方法可以实现部分不缓存
已声明方式使用Substitution控件
<asp:Substitution   ID= "Substitution1 "   runat= "server "   MethodName= "GetCurrentDateTime "   />
public   static   string   GetCurrentDateTime(HttpContext   context)
{
return   DateTime.Now.ToString();
}
//
方法签名必须和委托签名一致
以编程的方式使用Substitution控件API
Response.WriteSubstitution(new   HttpResponseSubstitutionCallback(GetCurrentDateTime))
以隐式方式使用AdRotator控件
这个控件永远都是不缓存的

SQL   Server依赖的缓存,非常之有用
当表数据发生改变就清除缓存

1
、为SQL   Server启用缓存通知
aspnet_regsql.exe   -S   <Server>   -U   <Username>   -P   <Password>
-ed   -d   Northwind   -et   -t   Employees
Server
:服务器
Username
:用户名
Password
:密码
Northwind
:数据库
Employees
:表
2
、为缓存功能配置网页
<%@OutputCache   Duration= "3600 "   SqlDependency= "Northind:Employees "   VaryByParam= "none "%>
3
、在Web.config文件中设置缓存配置
<caching>
  <sqlCacheDependency   enabled= "true "   pollTime= "1000 ">
    <database>
      <add   name= "Northind "   connectionStringName= "... "   pollTime   =   "1000 "   />
      </database>
  </sqlCacheDependency>
</caching>
//
这里的name要是数据库名称

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值