ASP.NET缓存机制则解释

2012 年 10 月 18 日4000

欢迎进入.NET社区论坛,与300万技术人员互动交流 >>进入

ASP.NET缓存机制解释

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

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

ASP.NET缓存机制概念㈠:
页输出缓存

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);

[1]

【责编:peter】

0 0