有一个查看用户资料的页(根据ID不同显示不同的用户信息),根据网友sp1234的启发,设置页面缓存
<%@ OutputCache Duration="3600" VaryByParam="id" VaryByCustom="@cache_usermsgchange" %>
注:VaryByParam="id" (id相同缓存一小时),VaryByCustom="@cache_usermsgchange" (@cache_usermsgchange此值不变缓存一小时)
在global.asax中有:(设置当值@cache_usermsgchange改变时清除缓存)
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom.StartsWith("@cache_"))
{
var val = context.Cache[custom];
if (val == null)
return string.Empty;
else
return (string)val;
}
return base.GetVaryByCustomString(context, custom);
}
然后在用户的个人资料修改页面设置 this.Cache["@cache_usermsgchange"] = DateTime.Now.Ticks.ToString();
这样设置之后,只要个人资料不修改,页面一小时内都缓存,暗自窃喜。
可是接下来发现问题了,
http://localhost:3471/WebSite1/person.aspx?id=18403040
本来是希望18403040这个用户的资料修改了,才清除缓存,但现在是所有的用户,只要有一个用户修改了资料,缓存就清除,自己比较菜,解决不了,所以再次上来求教希望sp1234能看到这个贴
<%@ OutputCache Duration="3600" VaryByParam="id" VaryByCustom="@cache_usermsgchange" %>
注:VaryByParam="id" (id相同缓存一小时),VaryByCustom="@cache_usermsgchange" (@cache_usermsgchange此值不变缓存一小时)
在global.asax中有:(设置当值@cache_usermsgchange改变时清除缓存)
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom.StartsWith("@cache_"))
{
var val = context.Cache[custom];
if (val == null)
return string.Empty;
else
return (string)val;
}
return base.GetVaryByCustomString(context, custom);
}
然后在用户的个人资料修改页面设置 this.Cache["@cache_usermsgchange"] = DateTime.Now.Ticks.ToString();
这样设置之后,只要个人资料不修改,页面一小时内都缓存,暗自窃喜。
可是接下来发现问题了,
http://localhost:3471/WebSite1/person.aspx?id=18403040
本来是希望18403040这个用户的资料修改了,才清除缓存,但现在是所有的用户,只要有一个用户修改了资料,缓存就清除,自己比较菜,解决不了,所以再次上来求教希望sp1234能看到这个贴
var val = context.Cache[key];