ASP.NET 2.0 中的缓存操作

本文详细介绍了ASP.NET 2.0中的多种缓存技术,包括页面缓存配置、代码设置缓存属性、文件及数据表依赖缓存、浏览器及参数缓存差异化、局部缓存排除以及应用程序级缓存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)、在ASP.NET 2.0 中增加了缓存技术 ,不尽可以支持1.x的模式,如:

<%@ OutputCache Duration="60" VaryByParam="None" Location=“Any” %>
其中“Location”可以是:Any / Client / DownStream / Server / ServerAndClient / None

(二)、还支持缓存配置,在Web.config中定义如:

<caching>
    
<outputCacheSettings>
        
<outputCacheProfiles>
            
<add name="Cache30Seconds" duration="30" varyByParam="none" />
        
</outputCacheProfiles>
    
</outputCacheSettings>
</caching>

在页面中引用某项缓存配置:

<%@ OutputCache CacheProfile="Cache30Seconds" %>

(三)、也可以通过代码方式设置缓存性:

Response.Cache.SetCacheability(HttpCacheability.Public);

其中“Public”对应“Any”、“Private”对应“Client”等。

(四)、文件依赖缓存:

通过 Response.AddFileDependencyPath / AddFileDependencies 将某个文件的(绝对)路径添加为缓存失效的判断条件,如:

protected void Page_Load(object sender, EventArgs e)
{
string fileDependencyPath = Server.MapPath("TextFile1.txt");
Response.AddFileDependency(fileDependencyPath);
// Set additional properties to enable caching.
Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(
true);
}

(五)、数据表依赖缓存

1.为SQL Server 启用缓存通知
C:/WINDOWS/Microsoft.NET/Framework/v2.0.40217
aspnet_regsql.exe -S <Server> -U <Username> -P <Password> -ed -d Northwind -et -t Employees

2.为缓存功能配置网页
<%@ OutputCache Duration="3600" SqlDependency="Northwind:Employees" VaryByParam="n

3.在Web.config 文件中设置缓存配置

<caching>
<sqlCacheDependency enabled = "true" pollTime = "1000" >
<databases>
<add name="Northwind" connectionStringName="NorthwindConnectionString1" pollTime = "1000"/>
</databases>
</sqlCacheDependency>
</caching>

(六)、浏览器不同缓存不同版本

<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="browser" %>

Response.Cache.SetExpires(DateTime.Now.AddMinutes(1d));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
Response.Cache.SetVaryByCustom("browser");

(七)、使用参数缓存不同版本

<%@ OutputCache Duration="60" VaryByParam="City" %>

Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
Response.Cache.VaryByParams["Zip"] = true;

(八)、使用Substitution控件 实现在缓存页面中局部不缓存

(九)、应用程序缓存

 

Cache["CacheItem1"= "Cached Item 1";
Cache.Insert(
"CacheItem2""Cached Item 2");
string[] dependencies = { "CacheItem2" };
Cache.Insert(
"CacheItem3""Cached Item 3"new System.Web.Caching.CacheDependency(null, dependencies));
Cache.Insert(
"CacheItem4""Cached Item 4"new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile1.xml")));
System.Web.Caching.CacheDependency dep1 
= new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));
string[] keyDependencies2 = { "CacheItem1" };
System.Web.Caching.CacheDependency dep2 
= new System.Web.Caching.CacheDependency(null, keyDependencies2);
System.Web.Caching.AggregateCacheDependency aggDep 
= new System.Web.Caching.AggregateCacheDependency();
aggDep.Add(dep1);
aggDep.Add(dep2);
Cache.Insert(
"CacheItem5""Cached Item 5", aggDep);

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值