35

如何清除asp.net中的服务器缓存?我发现有两种缓存。有浏览器缓存和服务器缓存。我已经进行了一些搜索,但我还没有找到使用 asp.net(或不使用)清除服务器缓存的清晰分步指南。

(更新)我刚刚了解到,它的代码隐藏在 VB - Visual Basic (dot net) 中。

4

7 回答 7

49

您可以遍历所有缓存项并一一删除:

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);
}
于 2013-05-13T22:29:06.613 回答
8

迭代存在一个问题:它不是线程安全的。如果您正在迭代,并且从另一个线程访问缓存,您可能会遇到错误。这种可能性很低,但对于高负载应用程序来说是个问题。仅供参考,一些缓存实现甚至不提供迭代方法。

此外,如果您正在清除缓存项目,您不想清除应用程序域的每个部分的所有内容,而只是清除与您相关的内容。

当我遇到这个问题时,我通过向所有缓存条目添加自定义 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
于 2014-07-01T21:53:17.060 回答
3
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]);
} 
于 2017-09-11T11:13:27.097 回答
2

您需要删除已添加到缓存中的项目:

var itemsInCache= HttpContext.Current.Cache.GetEnumerator();

while (itemsInCache.MoveNext())
{

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

}
于 2013-05-13T22:29:22.440 回答
2

我不确定您想要完成此操作的确切方法。但是有几种方法,一种方法是 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");
    }
}

这将成功回收您的应用程序池。这将清除缓存。你有几个选择。请注意,虽然这会清除缓存,但它也会终止任何存在的会话。

希望这会有所帮助。

于 2013-05-13T22:35:28.687 回答
1

System.Web.HttpRuntime.UnloadAppDomain() - 重新启动 Web 应用程序,清除缓存,重置 css/js 包

于 2016-11-29T19:21:29.300 回答
0

在页面加载事件中添加此代码 ..即 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")
于 2019-01-29T11:59:24.560 回答