2013-05-13 51 views
24

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

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

+1

我想嘗試在IIS中重新啓動站點,或者可能回收應用程序池。否則,您可能會公開一個手動刪除緩存中所有內容的頁面。 – millimoose 2013-05-13 22:28:35

+0

@xarzu在什麼情況下你想清除緩存? – 2014-12-06 21:32:35

回答

36

你可以通過所有的緩存項和循環刪除逐一:

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

+1 @Kenneth,但我認爲你的意思是'foreach'和'entry'而不是'de'。 – itsme86 2013-05-13 22:31:07

+0

是的,不知道我是怎麼做到的:-) – Kenneth 2013-05-13 22:32:07

+0

你打敗了我的問題的那部分,所以我把它從我的答案中敲除。很好的答案。 +1 – Greg 2013-05-13 22:38:16

1

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

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

while (itemsInCache.MoveNext()) 
{ 

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

} 
+1

其中是enumerator.Key集的值? – SeanKPS 2017-07-05 14:42:42

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

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

希望這會有所幫助。

+2

是的,如果您重置服務器,它也將清除緩存。這對於OP想要的東西有點矯枉過正 – Kenneth 2013-05-13 22:39:08

+0

@Kenneth是的,但我不確定他是如何完成清晰的。或者他會採用什麼樣的環境。我認爲我會給出一種可行的替代方法,只要他將特定的應用參數傳遞給它。它只會清除一個。 – Greg 2013-05-13 22:41:43

+1

您應該在答案中添加一條註釋,通過這個註釋,您還可以查看任何會話,Application對象中的所有數據以及正在執行的所有請求。 – Kenneth 2013-05-13 22:50:48

7

迭代有一個問題:它不是線程安全的。 如果您正在迭代,並且從另一個線程訪問緩存,則可能會出現錯誤。 這個概率很低,但是對於高負載應用來說這是一個問題。僅供參考,一些緩存實現甚至不提供迭代方法。另外,如果您正在清除緩存項目,則不需要清除應用程序域的每個部分的所有內容,而只是與您相關的內容。

當我遇到這個問題時,我通過爲我的所有緩存條目添加自定義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 
+0

或者只是使用「鎖定(HttpContext.Current.Cache)」使其線程安全。 – Undercover1989 2017-07-20 09:42:56

1

System.Web.HttpRuntime.UnloadAppDomain() - 重新啓動網絡應用程序,清除高速緩存,重置的CSS/JS束

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

添加說明文字以及答案肯定會幫助其他用戶提出類似的問題。請考慮添加... – 2017-09-11 11:32:57