如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面(二)
2006-08-27 19:08
956 查看
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的某一分页显示的数据页的请求时,将调用缓存(自然是已经缓存了该页)。
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的某一分页显示的数据页的请求时,将调用缓存(自然是已经缓存了该页)。
相关文章推荐
- 如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面
- 如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面
- @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面
- VaryByParam,VaryByHeader, VaryByControl, VaryByCustom OutputCache Directive Attributes
- Ubuntu下同时安装了python2版本和python3版本如何使用pip分别为P2和P3安装不同类库
- 如何使用 AllowPartiallyTrustedCallers 属性来调用具有一个强名称从 Web 页通过使用 Visual C#.NET、 Visual C# 2005年或更高版本的程序集
- 如何在项目里面使用freemarker实现页面缓存(三)
- 使用SqlCacheDependency的一点注记(如何开启Sqlserver2000的缓存机制)
- 在同一个页面使用多个不同的jQuery版本,让它们并存而不冲突
- OutputCache Duration 页面缓存参数
- WP_使用_FastCGI_Cache_实现高效页面缓存
- Yii 2.0如何使用页面缓存方法示例
- ASP.NET 页面缓存 @ OutputCache
- 《partner4java 讲述Spring入门》之:spring cache支持(spring3.1如何使用cache 缓存)
- WP_使用_FastCGI_Cache_实现高效页面缓存
- 如何在各个版本的VC及64位下使用CPUID指令
- 如何使用数据查询的Parameters中的output属性取的返回值
- 如何使用ListView实现一个带有网络请求,解析,分页,缓存的公共的List页面来大大的提高工作效率
- npm安装全局和局部包版本不同时,如何使用到局部包版本(举例webpack)
- 页面控件值不能回传(asp.net不同版本控件ReadOnly属性导致值回传问题)