如何在asp.net中清除服務器緩存?

[英]How do I clear the server cache in asp.net?


How do I clear the server cache in asp.net? I have found out that there are two kinds of cache. There is the browser cache and the server cache. I have done some searching but I have yet to find a clear, step-by-step guide for clearing the server cache using asp.net (or not).

如何在asp.net中清除服務器緩存?我發現有兩種緩存。有瀏覽器緩存和服務器緩存。我已經做了一些搜索,但我還沒有找到一個清晰的,逐步指導清除服務器緩存使用asp.net(或不)。

(update) I just learned that the code-behind for this is in VB - Visual Basic (dot net).

(更新)我剛學會了這個代碼隱藏在VB - Visual Basic(dot net)中。

6 个解决方案

#1


39  

You could loop through all the cache items and delete them one by one:

您可以循環遍歷所有緩存項並逐個刪除它們:

foreach (System.Collections.DictionaryEntry entry in HttpContext.Current.Cache){
    HttpContext.Current.Cache.Remove(string(entry.Key));
}

Syntax Correction for ASP.NET 4.5 C#

ASP.NET 4.5 C#的語法更正

foreach (System.Collections.DictionaryEntry entry in HttpContext.Current.Cache){
    HttpContext.Current.Cache.Remove((string)entry.Key);
}

#2


7  

There is a problem with iteration: it's not thread safe. If you are iterating, and the cache gets accessed from another thread, you might be getting an error. The probability of this is low, but it's a problem with high load applications. FYI, some cache implementations don't even provide iteration methods.

迭代存在問題:它不是線程安全的。如果您正在迭代,並且從另一個線程訪問緩存,則可能會收到錯誤。這種可能性很低,但這對高負載應用來說是一個問題。僅供參考,一些緩存實現甚至不提供迭代方法。

Also, if you are clearing your cache items, you don't want to clear everything from every part of the app domain, but just what's related to you.

此外,如果要清除緩存項目,則不希望清除應用程序域的每個部分中的所有內容,而是清除與您相關的內容。

When I faced this problem, I solved it by adding a custom CacheDependency to all my cache entries.

當我遇到這個問題時,我通過向所有緩存條目添加自定義CacheDependency來解決它。

This is how the CacheDependency is defined:

這是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

#3


2  

I'm not sure of the exact methodology in which you would like to accomplish this. But there are a few ways, one way is the one Giorgio Minardi posted which comes from this question.

我不確定您希望實現此目標的確切方法。但有幾種方法,一種方式是Giorgio Minardi發布的一個來自這個問題。

The other choices could be like this:

其他選擇可能是這樣的:

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

That will successfully recycle your application pool. Which would clear the cache. You've got a few choices. Beware, though this will clear the cache it will also terminate any sessions that exists.

這將成功回收您的應用程序池。哪個會清除緩存。你有幾個選擇。請注意,雖然這將清除緩存,但它也將終止任何存在的會話。

Hopefully this helps.

希望這會有所幫助。

#4


1  

You'll need to remove the items you've added to the cache:

您需要刪除已添加到緩存中的項目:

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

while (itemsInCache.MoveNext())
{

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

}

#5


1  

System.Web.HttpRuntime.UnloadAppDomain() - restarts web application, clears cache, resets css/js bundles

System.Web.HttpRuntime.UnloadAppDomain() - 重啟Web應用程序,清除緩存,重置css / js包

#6


1  

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/05/13/720d48f7a9e1348549915bf8daaffd28.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com