C# 如何清除asp.net中的服务器缓存?
如何清除asp.net中的服务器缓存?我发现有两种缓存。有浏览器缓存和服务器缓存。我已经做了一些搜索,但我还没有找到一个清晰的,逐步使用asp.net(或不使用)清除服务器缓存的指南C# 如何清除asp.net中的服务器缓存?,c#,asp.net,vb.net,caching,C#,Asp.net,Vb.net,Caching,如何清除asp.net中的服务器缓存?我发现有两种缓存。有浏览器缓存和服务器缓存。我已经做了一些搜索,但我还没有找到一个清晰的,逐步使用asp.net(或不使用)清除服务器缓存的指南 (更新)我刚刚了解到,这背后的代码是用VB-Visual Basic(dot net)编写的 您可以循环浏览所有缓存项并逐个删除它们: foreach (System.Collections.DictionaryEntry entry in HttpContext.Current.Cache){ HttpC
(更新)我刚刚了解到,这背后的代码是用VB-Visual Basic(dot net)编写的 您可以循环浏览所有缓存项并逐个删除它们:
foreach (System.Collections.DictionaryEntry entry in HttpContext.Current.Cache){
HttpContext.Current.Cache.Remove(string(entry.Key));
}
ASP.NET 4.5 C的语法更正#
您需要删除已添加到缓存中的项目:
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");
}
}
这将成功回收您的应用程序池。这将清除缓存。你有几个选择。注意,虽然这会清除缓存,但也会终止任何存在的会话
希望这有帮助。迭代有一个问题:它不是线程安全的。 如果您正在迭代,并且缓存是从另一个线程访问的,则可能会出现错误。 出现这种情况的概率很低,但这是高负载应用程序的一个问题。 仅供参考,一些缓存实现甚至不提供迭代方法 此外,如果你正在清除缓存项,你不想清除应用程序域每个部分的所有内容,只想清除与你相关的内容 当我遇到这个问题时,我通过向所有缓存项添加自定义CacheDependency来解决它 这就是CacheDependency的定义方式:
public 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
公共类CustomCacheDependency:CacheDependency
{
//调用此方法使缓存项过期:
public void ForceDependencyChange()
{
this.NotifyDependencyChanged(this,EventArgs.Empty);
}
}
//以下是我将对象添加到缓存的方式:
HttpContext.Current.Cache.Add(键,//唯一键
obj,
CreateNewDependency(),//分配依赖项的工厂方法
System.Web.Caching.Cache.NoAbsoluteExport,
新的时间跨度(0,0,ExpirationInSeconds),
System.Web.Caching.CacheItemPriority.Default,
ReportRemovedCallback);
//包含所有CustomCacheDependency对象的列表:
#区域依赖关系管理
私有列表dep_List=新列表();
私有CustomCacheDependency CreateNewDependency()
{
CustomCacheDependency dep=新的CustomCacheDependency();
锁(副清单)
{
dep_列表。添加(dep);
}
返回dep;
}
//调用此方法以线程安全方式仅刷新缓存项:
私有void FlushCache()
{
锁(副清单)
{
foreach(dep_列表中的CustomCacheDependency dep)dep.ForceDependencyChange();
dep_list.Clear();
}
}
#端区
System.Web.HttpRuntime.UnloadAppDomain()-重新启动Web应用程序,清除缓存,重置css/js捆绑包
public void ClearCacheItems()
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]);
}
{
列表键=新列表();
IDictionaryEnumerator enumerator=Cache.GetEnumerator();
while(枚举数.MoveNext())
Add(enumerator.Key.ToString());
对于(int i=0;i
在页面加载事件中添加此代码。这是清除缓存的http头
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")
我会尝试在IIS中重新启动站点,或者回收应用程序池。否则,您可能会公开一个手动删除缓存中所有内容的页面。@xarzu在什么情况下要清除缓存?+1@Kenneth,但我认为您的意思是
foreach
和entry
,而不是de
。是的,我不知道我是怎么做到的:-)你在我的问题上比我强,所以我把它从我的答案中删掉了。回答得好+1这种方法不也会有上面答案中提到的线程安全问题吗?我们的web api应用程序有一个存储在HttpContext.Current缓存中的对象列表。我们通过调用HttpContext.Current.cache.Insert(缓存键,我们的列表,…),将数据放入缓存(如果缓存当前==null)。我们希望为自己提供一个web api方法,该方法可以清除/重置缓存,如果需要,我们可以手动提交该缓存。我们正在方法中调用HttpContext.Current.Cache.Remove(CacheKey)。它似乎起作用了。这样做是否有问题?是的,如果重置服务器,它也会清除缓存。这对OP来说有点过分了wants@Kenneth是的,但我不确定他想如何完成任务。或者他会在什么环境下做。我想我会给出一种仍然可行的替代方法,只要他将特定的应用程序参数传递给它。这只会清除一个。你应该在你的答案中添加一个注释,这样你也会终止任何会话,您在应用程序对象中拥有的所有数据以及正在执行的任何请求。这是一段很好的代码,可以满足其他需要。我有一个更复杂的示例,用于使用绑定来构建应用程序池和网站。enumerator.Key set的值在哪里?或者只使用“lock(HttpContext.Current.Cache)”添加一个解释性的文本和你的答案肯定会帮助其他有类似问题的用户。请考虑加入……这是一个很好的解决方案。只需注意您拥有的任何逐出代码,因为它也将在删除每个项目时执行。
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")