使用自定义字符串对页的各个版本进行缓存GetVaryByCustomString
广告:
使用自定义字符串对页的各个版本进行缓存:
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
Response.Cache.SetVaryByCustom("minorversion");
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
Response.Cache.VaryByHeaders["Accept-Language"] = true;
}
<%@ outputcache duration="60" varybyparam="none" varybyheader="accept-language" %>
VaryByHeader-- 更据HTTP头信息改变缓存区内容(不支持用户控件输出缓存)
<%@ OutputCache Duration="#ofseconds"
Location="Any | Client | Downstream | Server | None |
ServerAndClient "
Shared="True | False"
VaryByControl="controlname"
VaryByCustom="browser | customstring"
VaryByHeader="headers"
VaryByParam="parametername"
CacheProfile="cache profile name | ''"
NoStore="true | false"
SqlDependency="database/table name pair | CommandNotification"
%>
VaryByCustom
表示自定义输出缓存要求的任意文本。如果赋予该属性的值为 browser,缓存将随浏览器名称和主要版本信息的不同而异。如果输入自定义字符串,则必须在应用程序的 Global.asax 文件中重写 GetVaryByCustomString 方法。
VaryByControl
一个分号分隔的字符串列表,用于更改用户控件的输出缓存。这些字符串代表用户控件中声明的 ASP.NET 服务器控件的 ID 属性值
VaryByCustom="UserID"自定义
第一个:
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 "UserID" :
if (Request.IsAuthenticated == true) {
cacheKey = "UserID_In";
}
else {
cacheKey = "UserID_Out";
}
break;
}
return cacheKey;
}
第二个:
global.asax 文件中:
<script>
public override string GetVaryByCustomString(HttpContext context, string arg){
If (arg = "minorversion "){
Return "Version= " + context.Request.Browser.MinorVersion.ToString();
}
}
</script>
第三个:
<%@ Application Language="C#" %>
<script runat="server">
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg.ToLower() == "prefs ")
{
HttpCookie cookie = context.Request.Cookies["Language"];
if (cookie != null)
{
return cookie.Value;
}
}
return base.GetVaryByCustomString(context, arg);
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
</script>
广告: