[转]缓冲技术探讨(三)

2.1.1.2 使用缓存回调(Cache Callback)
你可以定义回调,这样当移除自动发生时, 你可以不移除它或者使用新的数据来替换它。如:
 1CacheItemRemovedCallback onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
 2Cache.Insert("CachedItem",item,null,Cache.NoAbsoluteExpiration,
 3Cache.NoSlidingExpiration,CacheItemPriority.Default,
 4onRemove);
 5// Implement the function to handle the expiration of the cache.
 6public void RemovedCallback(string key, object value, CacheItemRemovedReason r)
 7{
 8   // Test whether the item is expired, and reinsert it into the cache.
 9   if (r == CacheItemRemovedReason.Expired)
10   {
11      // Reinsert it into the cache again.
12      CacheItemRemovedCallback onRemove = null;
13      onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
14      Cache.Insert(key,value,null,Cache.NoAbsoluteExpiration,
15Cache.NoSlidingExpiration,CacheItemPriority.Default,
16onRemove);
17   }

18}
2.1.1.3 对缓存项使用优先级
当运行应用程序的服务器内存不足时,会自动清除缓存中的数据,称为“清除scavenging”。此时,Cache对象根据缓存项的优先级来决定先移除哪些缓存数据,你可以在代码中指定缓存项的优先级。参看MSDN中“CacheItemPriority 枚举”,如:
Cache.Insert("DSN", connectionString, null, d, t, CacheItemPriority.High, onRemove);
2.1.1.4 刷新数据(清除缓存)
没有直接的方法来刷新Asp.net的输出缓存,但是有替代方法(设置所有数据失效),比如:
Response.Cache.SetExpires(DateTime.Now)

这可以清除缓存,但页面上并不立刻体现出来,直到最初的缓存期结束,比如:
<%@ OutputCache Duration="10" VaryByParam="none" %>指令指定的缓存只会在10秒后才清除。通常并不需要清除所有缓存项,你只要重新加载数据更新缓存就够了

2.1.2  输出缓存(Output Cache)
你可以使用两种方式的输出缓存来缓存需要传输和显示到客户端浏览器上的数据——页面输出缓存(Page Output Cache)和页面片断缓存(Page Fragment Cache)。当整个页面相对变化较少时,可以缓存整个页面;如果只是页面的一部分经常变化,可以使用片断缓存。

2.1.2.1 页面输出缓存
Page Output Caching将对页面请求的响应放入缓存中,后续对此页面的请求将直接从缓存中得到信息而不是重建此页面。可以通过添加Page指令(高级别,声明实现)来实现,也可以使用HTTPCachePolicy类来实现(低级别,程序实现)。本指南不打算介绍技术细节,只给出如何更好使用的指南和最佳实践。有四方面的内容:

1、 决定缓存的内容

页面输出缓存可缓存各种信息,缓存这些信息意味着你不需要经常处理同样的数据和结果,包括:
a·经常被请求但不不改变的静态页面;
b·更新频率和时间已知的页面(如显示股票价格的页面);
c·根据HTTP参数,有几个可能输出的页面(如根据城市的代号显示该城市天气情况的页面);

·从Web Service返回的结果;如:

1[WebMethod(CacheDuration=60)]
2public string HelloWorld()
3{
4    return "Hello World";
5}

6
7

2、 缓存动态页面

基于输入参数、语言和浏览器类型改变的动态网页经常用到。你可以使用OutputCache的以下属性来实现对动态页面的缓存:
VaryByParam——基于输入参数不同缓存同一页面的多个版本;
VaryByHeader——基于Page Header的内容不同缓存页面的多个版本;
VaryByCustom——通过声明属性和重载GetVaryByCustomString方法来定制缓存处理页面的多个版本;
VaryByControl——基于控件中asp对象属性的不同来缓存控件。
对多个版本页面的缓存会降低可用内存,所以要仔细衡量缓存策略。s

3、 控制缓存的位置
你可以使用@OutputCache指令的OutputCacheLocation属性的枚举值来指定缓存的位置,如:
<%@ outputcache duration="10" varybyparam="none" Location="Server" %>

4、 配置页面输出缓存
有两种方式控制,你可以使用Page指令,也可以使用Cache API编程实现。参考以下两段代码:
//代码1,使用指令
<%@ OutputCache Duration="20" Location="Server" VaryByParam="state" VaryByCustom="minorversion" VaryByHeader="Accept-Language"%>
//代码2,编程实现

 1private void Page_Load(object sender, System.EventArgs e)
 2{
 3   // Enable page output caching.
 4   Response.Cache.SetCacheability(HttpCacheability.Server);
 5   // Set the Duration parameter to 20 seconds.
 6   Response.Cache.SetExpires(System.DateTime.Now.AddSeconds(20));
 7   // Set the Header parameter.
 8   Response.Cache.VaryByHeaders["Accept-Language"= true;
 9   // Set the cached parameter to 'state'.
10   Response.Cache.VaryByParams["state"= true;
11   // Set the custom parameter to 'minorversion'.
12   Response.Cache.SetVaryByCustom("minorversion");
13
14}

2.1.2.2 页面片断缓存
有时候缓存整个页面并不灵活,同时内存的发但也比较大,这时候应考虑片断缓存。页面片断缓存适合以下类型的数据:
·创建开销很大的页面片断(控件);
·包含静态数据的页面片断;
·可被多个用户使用的页面片断;
·多个页面共享的页面片断(如公用菜单条)
以下是缓存部分页面的例子:

1// Partial caching for 120 seconds
2[System.Web.UI.PartialCaching(120)]
3public class WebUserControl : System.Web.UI.UserControl
4{
5  // Your Web control code
6}

2.1.3  在非Web项目中使用Asp.net缓存
Asp.net Cache位于System.Web命名空间,但由于它是一个通用的方案,所以仍然可以在引用此命名空间的任何非Web项目中使用它。
System.Web.Caching.Cache 类是对象的缓存,它可以通过System.Web.HttpRuntime.Cache 的静态属性或System.Web.UI.Page 和System.Web.HttpContext.Cache来访问。因此在请求上下文之外也可以存在,在每个应用程序域中只有一个实例,所以HttpRuntime.Cache对象可以在Aspnet_wp.exe之外的每个应用程序域中存在。以下代码演示了在普通应用里访问Cache对象:

1HttpRuntime httpRT = new HttpRuntime();
2Cache cache = HttpRuntime.Cache;
posted on 2008-06-18 19:38  Xuemin_Zhang  阅读(235)  评论(0编辑  收藏  举报