如何清除asp.net中的服务器缓存?我发现有两种缓存:浏览器缓存和服务器缓存。我进行了一些搜索,但尚未找到清除使用asp.net(或其他方式)清除服务器缓存的明确、分步指南。
(更新)我刚刚得知此代码后端是VB - Visual Basic(.net)。
foreach (System.Collections.DictionaryEntry entry in HttpContext.Current.Cache){
HttpContext.Current.Cache.Remove(string(entry.Key));
}
ASP.NET 4.5 C#的语法纠正
foreach (System.Collections.DictionaryEntry entry in HttpContext.Current.Cache){
HttpContext.Current.Cache.Remove((string)entry.Key);
}
foreach
和entry
而不是de
。 - itsme86public class CustomCacheDependency : CacheDependency
{
//this method is called to expire a cache entry:
public void ForceDependencyChange()
{
this.NotifyDependencyChanged(this, EventArgs.Empty);
}
}
//this is how I add objects to cache:
HttpContext.Current.Cache.Add(key, //unique key
obj,
CreateNewDependency(), //the factory method to allocate a dependency
System.Web.Caching.Cache.NoAbsoluteExpiration,
new TimeSpan(0, 0, ExpirationInSeconds),
System.Web.Caching.CacheItemPriority.Default,
ReportRemovedCallback);
//A list that holds all the CustomCacheDependency objects:
#region dependency mgmt
private List<CustomCacheDependency> dep_list = new List<CustomCacheDependency>();
private CustomCacheDependency CreateNewDependency()
{
CustomCacheDependency dep = new CustomCacheDependency();
lock (dep_list)
{
dep_list.Add(dep);
}
return dep;
}
//this method is called to flush ONLY my cache entries in a thread safe fashion:
private void FlushCache()
{
lock (dep_list)
{
foreach (CustomCacheDependency dep in dep_list) dep.ForceDependencyChange();
dep_list.Clear();
}
}
#endregion
public void ClearCacheItems()
{
List<string> keys = new List<string>();
IDictionaryEnumerator enumerator = Cache.GetEnumerator();
while (enumerator.MoveNext())
keys.Add(enumerator.Key.ToString());
for (int i = 0; i < keys.Count; i++)
Cache.Remove(keys[i]);
}
var itemsInCache= HttpContext.Current.Cache.GetEnumerator();
while (itemsInCache.MoveNext())
{
HttpContext.Current.Cache.Remove(enumerator.Key);
}
我不确定您想要实现这个目标的确切方法。但是有几种方法,其中一种是Giorgio Minardi发布的方法,来自于这个问题。
其他选择可能是这样的:
using Microsoft.Web.Administration;
public bool RecycleApplicationPool(string appPoolName)
{
try
{
using (ServerManager iisManager = new ServerManager())
{
iisManager.ApplicationPools[appPoolName].Recycle();
return true;
}
}
catch (Exception ex)
{
throw new Exception("Unhandled Exception");
}
}
这将成功地回收您的应用程序池。这将清除缓存。你有几个选择。注意,虽然这将清除缓存,但也会终止任何已存在的会话。
希望这可以帮助你。
System.Web.HttpRuntime.UnloadAppDomain() - 重新启动 Web 应用程序,清除缓存,重置 CSS/JS 捆绑包
Response.CacheControl = "private"
Response.CacheControl = "no-cache"
Response.ClearHeaders()
Response.AppendHeader("Cache-Control", "no-cache")
Response.AppendHeader("Cache-Control", "private")
Response.AppendHeader("Cache-Control", "no-store")
Response.AppendHeader("Cache-Control", "must-revalidate")
Response.AppendHeader("Cache-Control", "max-stale=0")
Response.AppendHeader("Cache-Control", "post-check=0")
Response.AppendHeader("Cache-Control", "pre-check=0")
Response.AppendHeader("Pragma", "no-cache")
Response.AppendHeader("Keep-Alive", "timeout=3, max=993")
Response.AppendHeader("Expires", "Mon, 26 Jul 2006 05:00:00 GMT")