ASP.NET数据缓存机制浅析

开发 后端
ASP.NET 数据缓存机制是什么呢?主要涉及什么方面呢?那么本文就向你介绍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. {   
  4. return   DateTime.Now.ToString();   
  5. }   
  6. //方法签名必须和委托签名一致  

②以编程的方式使用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数据缓存有所帮助。

【编辑推荐】

  1. ASP.NET编程规范之编码规范浅析
  2. ASP.NET编程规范之测试规范浅析
  3. ASP.NET的五大数据控件浅谈
  4. ASP.NET数据绑定控件比较浅析
  5. ASP.NET的GridView与DataGrid控件比较浅析
责任编辑:仲衡 来源: CSDN博客
相关推荐

2009-07-31 09:57:47

ASP.NET数据库缓

2009-08-04 15:22:33

ASP.NET缓存机制

2009-08-17 16:59:36

ASP.NET缓存机制

2009-07-31 09:47:57

ASP.NET缓存

2009-07-24 11:35:08

2009-04-08 09:58:07

ASP.NET MVCTempData框架

2009-07-31 09:32:04

ASP.NET缓存概念ASP.NET缓存应用

2009-10-10 10:44:15

ASP.NET页面缓存

2009-08-10 14:38:29

ASP.NET组件设计

2009-08-03 18:47:12

ASP.NET数据缓存

2009-07-31 12:43:59

ASP.NET MVC

2009-08-05 15:50:13

ASP.NET优点

2009-07-24 13:41:15

ASP.NET AJA

2009-08-05 18:36:12

ASP.NET Che

2010-08-06 08:50:21

ASP.NET

2009-08-10 13:32:15

ASP.NET TimASP.NET组件设计

2009-07-31 10:23:44

缓存页面ASP.NET缓存

2009-08-04 15:20:59

ASP.NET数据验证数据验证控件

2009-08-07 15:34:15

ASP.NET数据绑定

2009-08-04 10:02:36

中国站长站
点赞
收藏

51CTO技术栈公众号