我有2个页面index.aspx 缓存设置的是 <%@ OutputCache Duration="3600" VaryByParam="none"%>find.aspx 参数有 c1 c2 c3 kw,我达到这样的效果 怎么写
c1 c2 c3 为 每个参数建立缓存,这个我知道可以VaryByParam="c1;c2;c3"
关键是参数kw,想要达到只要出现了这个参数就不缓存页面。还有就是在管理后台添加了新数据后,我想马上看结果,怎么在admin.aspx里去清除index.aspx的缓存啊?
c1 c2 c3 为 每个参数建立缓存,这个我知道可以VaryByParam="c1;c2;c3"
关键是参数kw,想要达到只要出现了这个参数就不缓存页面。还有就是在管理后台添加了新数据后,我想马上看结果,怎么在admin.aspx里去清除index.aspx的缓存啊?
{
if (Request.QueryString["kw"] != null)
{
Page.Response.Buffer = false;
Page.Response.Cache.SetNoStore();
}
}ASP.NET清除页面缓存 (1) Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "No-Cache");
(2) HTML方法
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</HEAD> (3) 重新调用原页面的时候在给页面传一个参数: href="****.ASPX?random()"
Cache[""]==null判断
参考
参考
通过缓存依赖实现数据更新或清楚缓存,再赋值
//不让浏览器缓存
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
Response.AddHeader("pragma", "no-cache");
Response.AddHeader("cache-control", "");
Response.CacheControl = "no-cache";
Response.ContentType = "text/plain";
然后在Global.asax代码中写public override string GetVaryByCustomString(HttpContext context, string custom)
{
switch (custom)
{
case "CheckKWExist":
return context.Request.QueryString["kw"] != null ? DateTime.Now.Ticks.ToString() : "";
default:
return base.GetVaryByCustomString(context, custom);
}
}asp.net缓存的目的跟浏览器缓存根本不同。它是为了避免页面重新计算。所以你要测试,如果清除了ie浏览器的客户端缓存(FireFox其实根本不管那一套)反而造成asp.net页面缓存失效,那么这个清除客户端缓存功能反而是画蛇添足。从谨慎考虑,在url中追加随机生成的参数串可以确保不会破坏asp.net页面缓存,是又安全又简练的写法。而写
this.Response.CacheControl = "no-cache";
或者
this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
这对asp.net页面缓存很可能是不安全的。
/MySite/Default.aspx?c1=aaaaa&rand=1235
/MySite/Default.aspx?c1=aaaaa&rand=1236此时不会重建页面。而如果/MySite/Default.aspx?c1=aaaaa&kw=1&rand=1234
/MySite/Default.aspx?c1=aaaaa&kw=1&rand=1235
/MySite/Default.aspx?c1=aaaaa&kw=1&rand=1236虽然kw参数不变,服务器每一次却都会重建页面。