如何在asp.net中清除服务器缓存?

时间:2013-05-13 22:23:48

标签: c# asp.net vb.net caching

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

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

7 个答案:

答案 0 :(得分:45)

您可以遍历所有缓存项并逐个删除它们:

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);
}

答案 1 :(得分: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

答案 2 :(得分:2)

我不确定您希望实现此目标的确切方法。但有一些方法,一种方式是Giorgio Minardi发布的一种方式来自question

其他选择可能是这样的:

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");
    }
}

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

希望这有帮助。

答案 3 :(得分:2)

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]);
} 

答案 4 :(得分:1)

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

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

while (itemsInCache.MoveNext())
{

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

}

答案 5 :(得分:1)

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

答案 6 :(得分: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")