@ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面

为每个session页配制缓存是通过 @ OutputCache 指令的属性 VaryByCustom 来实现的。按照下面的脚本(包含到一个.aspx文件中),花上10秒钟就可以为各个session ID用户配制独立的页面版本。

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

要用到 VaryByCustom="SessionID" ,就要在在服务器的根目录下有Global.asax文件,在文件中声明以下用法:

<script language="C#" runat="server">
  public override string GetVaryByCustomString(HttpContext context, string arg)
  {
      if (arg.ToLower () == "sessionid") {
          HttpCookie cookie =
           context.Request.Cookies["ASP.NET_SessionId"];
           if (cookie != null)
              return cookie.Value;
      }
      return base.GetVaryByCustomString (context, arg);
  }
</script>

GetVaryByCustomString 是ASP.NET页面输出缓存的基础。它继承于HttpApplication,返回从session cookie得到的session ID。用户在调用GetVaryByCustomString时会被关联到一个session。

这个技术的缺点是不能用于不支持cookie的用户。另外,用户只有在第二次发出申请时才能获得缓存,因为第一次请求没有合法的session cookie。

 

示例2;

 

petshop输出缓存设置
petshop
web页面上部的ControlHeader是随着用户登陆的状态有关的,故其设置了VaryByCustom属性以来标识用户不同登陆状态的缓存版本。而Category页面由于可能被大量的访问,并且数据量很大,是十分有必要缓存的,但是由于数据的随机性很大,存在不同的版本,比如说是不同类别的Category,甚至不同的分页显示的数据页,在这里采用了VaryByCustom属性以缓存不同版本的页。

我们再看下其具体实现代码:
VaryByCustom,我们可以自定义输出缓存要求的任意文本。除了在OutputCache指令里面申明该属性之外,我们还得在应用程序的 global.asax 文件的代码声明块中,重写 GetVaryByCustomString 方法来为自定义字符串指定输出缓存的行为。

举一列来说:

<%@ OutputCache VaryByParam="none" VaryByCustom="CategoryPageKey" Location="server" Duration="43200" %>

这里的VaryByCustom定义的为CategoryPageKey,那么在global.asax里面我们必须定义CategoryPageKey这个字符创输出缓存的行为,见下面代码。

 

代码
public override string GetVaryByCustomString(HttpContext context, String arg) { 

string cacheKey = ""

switch(arg) { 

case "CategoryPageKey"

if (Request.IsAuthenticated == true) { 

cacheKey 
= "QQQ" + context.Request.QueryString["category_id"+ context.Request.QueryString["requestedPage"]; 



else { 

cacheKey 
= "AAA" + context.Request.QueryString["category_id"+ context.Request.QueryString["requestedPage"]; 



break

case "SearchPageKey" : 

if (Request.IsAuthenticated == true) { 

cacheKey 
= "QQQ" + context.Request.QueryString["search_text"+ context.Request.QueryString["requestedPage"]; 



else { 

cacheKey 
= "AAA" + context.Request.QueryString["search_text"+ context.Request.QueryString["requestedPage"]; 



break

case "ProductPageKey" : 

if (Request.IsAuthenticated == true) { 

cacheKey 
= "QQQ" + context.Request.QueryString["name"+ context.Request.QueryString["product_id"+ context.Request.QueryString["requestedPage"]; 



else { 

cacheKey 
= "AAA" + context.Request.QueryString["name"+ context.Request.QueryString["product_id"+ context.Request.QueryString["requestedPage"]; 



break

case "ProductDetailsPageKey" : 

if (Request.IsAuthenticated == true) { 

cacheKey 
= "QQQ" + context.Request.QueryString["item_id"+ context.Request.QueryString["requestedPage"]; 



else { 

cacheKey 
= "AAA" + context.Request.QueryString["item_id"+ context.Request.QueryString["requestedPage"]; 



break

case "UserID" : 

if (Request.IsAuthenticated == true) { 

cacheKey 
= "UserID_In"



else { 

cacheKey 
= "UserID_Out"



break



return cacheKey; 



 

 

从上面对CategoryPageKey字符创所作的行为来看,当我们的请求页面中含有对特定的category_id的某一分页显示的数据页的请求时,将调用缓存(自然是已经缓存了该页)。

 

posted @ 2010-11-11 00:11  唔愛吃蘋果  阅读(719)  评论(0编辑  收藏  举报