如何在asp.net中强制清除缓存 [英] How to force clear cache in asp.net

查看:62
本文介绍了如何在asp.net中强制清除缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net中强制清除缓存

How to force clear cache in asp.net

推荐答案

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
 Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Response.Cache.SetNoStore();





将此代码放入页面加载。现在缓存浏览器中没有数据会尝试从serv获取页面



Put this code in page load. Now there will be no data in cache browser will try to get the page from the serv


我试试这个但是看起来不行吗



public void DisablePageCaching()

{

//用于禁用页面缓存

HttpContext.Current.Response.Cache.SetExpires( DateTime.UtcNow.AddDays(-1));

HttpContext.Current.Response.Cache.SetValidUntilExpires(false);

HttpContext.Current.Response.Cache.SetRevalidation( HttpCacheRevalidation.AllCaches);

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

HttpContext.Current.Response.Cache.SetNoStore();

HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));

}



private void ClearCache()

{

DictionaryEntry entry = default(DictionaryEntry);

foreach(System.Web.HttpContext.Current.Cache中的DictionaryEntry entry_loopVariable)

{

entry = entry_loopVariable;

System.Web.HttpContext.Current.Cache.Remove(entry.Key.ToString());

}



IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();



while(enumerator.MoveNext())

{

HttpContext.Current.Cache.Remove(enumerator.Key.ToString());

}

HttpContext.Current.Response.ClearHeaders();

HttpContext.Current.Response.Expires = 0;

HttpContext .Current.Response.CacheControl =no-cache;

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);

HttpContext.Current.Response。 Cache.SetNoStore();

HttpContext.Current.Response.Buffer = true;

HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1 ,0,0,0));

HttpContext.Current.Response.AppendHeader(Pragma,no-cac他);

HttpContext.Current.Response.AppendHeader(,);

HttpContext.Current.Response.AppendHeader(Cache-Control,no-cache); // HTTP 1.1

HttpContext.Current.Response.AppendHeader(Cache-Control,private); // HTTP 1.1

HttpContext.Current.Response.AppendHeader(Cache-Control,no-store); // HTTP 1.1

HttpContext.Current.Response.AppendHeader(Cache-Control,must-revalidate); // HTTP 1.1

HttpContext.Current.Response.AppendHeader(Cache-Control,max-stale = 0); // HTTP 1.1

HttpContext.Current.Response.AppendHeader(Cache-Control,post-check = 0); // HTTP 1.1

HttpContext.Current.Response.AppendHeader(Cache-Control,pre-check = 0); // HTTP 1.1

HttpContext.Current.Response.AppendHeader(Pragma,no-cache); // HTTP 1.1

HttpContext.Current.Response.AppendHeader(Keep-Alive,timeout = 3,max = 993); // HTTP 1.1

}
i try this but it seems not working

public void DisablePageCaching()
{
//Used for disabling page caching
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
}

private void ClearCache()
{
DictionaryEntry entry = default(DictionaryEntry);
foreach (DictionaryEntry entry_loopVariable in System.Web.HttpContext.Current.Cache)
{
entry = entry_loopVariable;
System.Web.HttpContext.Current.Cache.Remove(entry.Key.ToString());
}

IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();

while (enumerator.MoveNext())
{
HttpContext.Current.Cache.Remove(enumerator.Key.ToString());
}
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.CacheControl = "no-cache";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
HttpContext.Current.Response.Cache.SetNoStore();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ExpiresAbsolute =DateTime.Now.Subtract(new TimeSpan(1,0,0,0));
HttpContext.Current.Response.AppendHeader("Pragma", "no-cache");
HttpContext.Current.Response.AppendHeader("", "");
HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1
HttpContext.Current.Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1
}


如果你想设置当前页面的清除缓存,那么在按钮点击和页面加载时写下面的代码,它会工作对我来说。



HttpResponse.RemoveOutputCacheItem(/ pagecontentdetail.aspx)
If you want set clear cache of current page then write below code on button click as well as on page load, It will work for me.

HttpResponse.RemoveOutputCacheItem("/pagecontentdetail.aspx")


这篇关于如何在asp.net中强制清除缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆