ASP.NET数据缓存机制浅析

ASP.NET数据缓存机制主要是什么呢?让我们开始我们的讲解:

◆页输出缓存:保存页处理输出,下次重用所保存的输出

◆应用程序缓存:允许缓存所生成的数据,如DataSet

㈠ASP.NET数据缓存页输出缓存

1、ASP.NET数据缓存页输出缓存的几中形式

① ﹤%@   OutputCache   Duration= "60 "   VaryByParam= "None "   Location= "Any "%﹥

Location指定在哪个地方缓存,Any任何地方都缓存。

60秒以内看到的都是一样的了。

②还可在配置文件里写,然后在页面调用配置文件的缓存名称。

③用编程的方式:

 
 
 
  1. Response.Canche.SetExpires(DateTime.Now.AddSeconds(3)); 
  2. Response.Canche.SetCacheabiliy(HttpCacheability.Public); 
  3. Response.Canche.SetValidUntilExpires(true);

 相当于:

 
 
 
  1. Public   =﹥   Any 
  2. Private   =﹥   Client 
  3. NoCache   =﹥   None 
  4. Server   =﹥   Server 
  5. ServerAndPrivate   =﹥ ServerAndClient 

2、ASP.NET数据缓存使用文件依赖项缓存页输出

产生背景:有时候,可能需要在文件发生更改时从输出缓存中移除某一项。就是说文件改了以后缓存立即失效。

 
 
 
  1. string   filepath   =   Server.MapPath( "TextFile1.txt "); 
  2. Response.AddFileDependency(filepath);//添加缓存依赖项 
  3. Response.Cache.SetExpires(DateTime.Now.AddSeconds(60)); 
  4. Response.Cache.SetCacheability(HttpCacheability.Public); 
  5. Response.Cache.SetValidUntiExpires(true); 

3、ASP.NET数据缓存缓存多个版本

①使用请求的浏览器对页的各个版本进行缓存

 
 
 
  1. ﹤%@OutputCache   Duration= "10 "   VaryByParam= "None "   VaryByCustom= "browser "%﹥ 

②使用参数对页的各个版本进行缓存

 
 
 
  1. ﹤%@OutputCache   Duration= "60 "   VaryByParam= "City "%﹥ 

这个调试可以在url后加QueryString

如:...url?City=shanghai

程序里得到这个上海然后再做其他的操作,这个时候如果参数传的还是shanghai它就不会在走到程序里了。

4、ASP.NET数据缓存动态更新缓存页的部分,有三种方法可以实现部分不缓存

①已声明方式使用Substitution控件

 
 
 
  1. ﹤asp:Substitution   ID= "Substitution1 "   runat= "server "   MethodName= "GetCurrentDateTime "   /﹥ 
  2. public   static   string   GetCurrentDateTime(HttpContext   context) 
  3. return   DateTime.Now.ToString(); 
  4. //方法签名必须和委托签名一致 

②以编程的方式使用Substitution控件API

Response.WriteSubstitution(new   HttpResponseSubstitutionCallback(GetCurrentDateTime))

③以隐式方式使用AdRotator控件

这个控件永远都是不缓存的

㈡ASP.NET数据缓存SQL   Server依赖的缓存,非常之有用

当表数据发生改变就清除缓存

1、ASP.NET数据缓存为SQL   Server启用缓存通知

 
 
 
  1. aspnet_regsql.exe   -S   ﹤Server﹥   -U   ﹤Username﹥   -P   ﹤Password﹥ 
  2. -ed   -d   Northwind   -et   -t   Employees 

Server:服务器

Username:用户名

Password:密码

Northwind:数据库

Employees:表

2、ASP.NET数据缓存为缓存功能配置网页

 
 
 
  1. ﹤%@OutputCache   Duration= "3600 "   SqlDependency= "Northind:Employees "   VaryByParam= "none "%﹥ 

3、ASP.NET数据缓存在Web.config文件中设置缓存配置

 
 
 
  1. ﹤caching﹥ 
  2.   ﹤sqlCacheDependency   enabled= "true "   pollTime= "1000 "﹥ 
  3.     ﹤database﹥ 
  4.       ﹤add   name= "Northind "   connectionStringName= "... "   pollTime   =   "1000 "   /﹥ 
  5.       ﹤/database﹥ 
  6.   ﹤/sqlCacheDependency﹥ 
  7. ﹤/caching﹥ 

ASP.NET数据缓存方面的内容就向你介绍到这里,希望对你了解ASP.NET数据缓存有所帮助。

网站栏目:ASP.NET数据缓存机制浅析
文章转载:http://www.hantingmc.com/qtweb/news39/464739.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联