ASP.net缓存应用详细说明
1 使用页面输出缓存
<%@ OutputCache Duration=”60” VaryByParam=”none” %>
注:5分钟时可以可靠地缓存页面的最大时间量。
1.1 按参数改变缓存内容
VaryByParam参数值为none或*。
注:记住页面的每个缓存副本都会占用内存。
1.2 按头改变内容
VaryByHeader参数值为User-Agent,Referrer,Host或Language头。
1.3 按自定义的字符串改变缓存内容
VaryByCustom属性值为Browser或自定义字符串。
若用自定义字符串需要在Global.asax文件中添加
例:添加VBScript(根据请求浏览器是否支持VBScript来判断)
Overrides String GetVaraByCustomString( HttpContext object, String strCustom)
{
Return objContext.Request.Browser.VBScript;
}
1.4 设置缓存位置
Location属性值为Any,Client,Downstream,None,Server。
1.5 使用HttpCachePolicy类
其中:
SetExpires()方法指定页面过期的绝对日期和时间;
SetCacheability()方法指定应如何缓存页面。
SetCacheability()方法中的参数为HttpCacheability枚举值之一:NoCache,Private,Public,Server。
例:
private void Page_Load(object sender, System.EventArgs e)
{
Response.Cache.SetExpires( #01/01/2006# );
Response.Cache.SetCacheability(HttpCacheability.Server);
}
2 使用页面分段缓存
页面分段缓存是用用户控件实现的。
在用户控件中加 <%@ OutputCache Duration=”60” VaryByParam=”none” %>
2.1 按参数改变页面分段缓存
同1.1
2.2 页面分段缓存的限制
如为用户控件启动的缓存,就不再能在包含它的页面中通过程序访问此空间。
例:
声明一下缓存的用户控件:
<myControls:PageContents ID=”ctlContents” Runat=”Server” />
不能通过程序在页面中设置此用户控件的属性:
ctlContents.Message = “Hello!”;
不应该对缓存的用户控件使用数据绑定语法。
例:
< myControls:PageContents CategoryID=’<%# CategoryID %>’ Runat=”Server” />
3 使用页面数据缓存
3.1 在缓存中添加条目
Cache(“myItem”) = “Hello World!”;
Cache.Remove(“myItem”);
注:从缓存取条目时加判断 if( Cache(“myItem”) == null ){}
3.2 添加缓存文件依赖性
Cache.Insert( “myItem” , “Hello!” , new CacheDependency(MapPath( “myFile.txt” )) );
MapPath( “myFile.txt” ) 也可为String数组。
例:
private void Page_Load(object sender, System.EventArgs e)
{
DataSet dsFormat = null;
dsFormat = Cache(“FormatInfo”);
if(dsFormat == null)
{
dsFormat = getFormatInfo();
Cache.Insert( “FormatInfo” , dsFormat , new CacheDependency( MapPath( “FormatInfo.xml” ) ));
}
}
private DataSet getFormatInfo()
{
DataSet dsFormat = new DataSet();
dsFormat.ReadXML( MapPath( “FormatInfo.xml” ) );
return dsFormat;
}
3.3 添加缓存触发器依赖性
在数据库表中记录被修改时更新缓存中的条目:
UpdateCache.sql:
CREATE TRIGGER UpdateCache
ON Products
FOR UPDATE, DELETE, INSERT
AS
DECLARE @cmd Varchar(200)
SELECT @cmd = ‘echo ’ + Cast( getDATE() As Varrchar( 50 ) ) + ‘ > c:\tableChange.txt’
EXEC master..xp_cmdshell @cmd, no_output
.aspx:
private void Page_Load(object sender, System.EventArgs e)
{
DataSet dsProducts = null;
dsProducts = Cache(“productsDS”);
if(dsProducts == null)
{
dsProducts = getProducts();
Cache.Insert( “Products” , dsProducts , new CacheDependency( MapPath( “c:\tableChange.txt” ) ));
}
}
private DataSet getProducts()
{
DataSet dsProducts = new DataSet();
从数据库取出数据…
return dsProducts;
}
3.4 添加缓存键依赖性
String arrKeyDepends() = { “item2” };
Cache.Insert( “item1” , “Hello!” , new CacheDependency( Nothing, arrKeyDepends ));
3.5 建立绝对的过期策略
private void Page_Load(object sender, System.EventArgs e)
{
String strTime = null;
strTime = Cache(“time”);
if(strTime == null)
{
strTime = DataTime.Now.ToString();
Cache.Insert( “time” , strTime , Nothing , DateTime.Now.AddMinutes( 1 ) , Cache.NoAbsoluteExpiration );
}
}
3.6 建立变化的过期策略
private void Page_Load(object sender, System.EventArgs e)
{
String strTime = null;
strTime = Cache(“time”);
if(strTime == null)
{
strTime = DataTime.Now.ToString();
Cache.Insert( “time” , strTime , Nothing , DateTime.Now.AddMinutes( 1 ) , Cache.NoAbsoluteExpiration );
}
}
3.7 设置缓存条目优先级
Cache.Insert()方法的参数中:
CacheItemPriority枚举值为 NotRemovable,High,AboveNormal,Default,Normal,BelowNormal和Low。
CacheItemPriorityDecay枚举值为Never,Slow,Medium和Fast。
3.8 创建缓存回调方法
private void ItemRemoved( String itemKey, object itemValue, CacheItemRemovedReason removedReason )
{
…
}
//最后一个参数代表一个CacheItemRemovedReason枚举值:DependencyChanged,Expired,Removed或Underused。
private void btnAddCache_Click(object sender, System.EventArgs e)
{
Shared CacheItemRemovedCallback onRemove = new CacheItemRemovedCallback( AddressOf ItemRemoved );
Cache.Insert( “myItem”, “Hello!”, Nothing, Now.AddSeconds( 10 ), Cache.NoSlidingExpiration, CacheItemPriority.High, CacheItemPriorityDecay.Slow , onRemove );
}