如何清除asp.net中的服務器緩存?我發現有兩種緩存。有瀏覽器緩存和服務器緩存。我已經做了一些搜索,但我還沒有找到一個清晰的,逐步的指導,以清除使用asp.net服務器緩存(或不)。如何清除asp.net中的服務器緩存?
(更新)我剛剛得知這個代碼隱藏在VB - Visual Basic(dot net)中。
如何清除asp.net中的服務器緩存?我發現有兩種緩存。有瀏覽器緩存和服務器緩存。我已經做了一些搜索,但我還沒有找到一個清晰的,逐步的指導,以清除使用asp.net服務器緩存(或不)。如何清除asp.net中的服務器緩存?
(更新)我剛剛得知這個代碼隱藏在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#
foreach (System.Collections.DictionaryEntry entry in HttpContext.Current.Cache){
HttpContext.Current.Cache.Remove((string)entry.Key);
}
你需要刪除的項目你已添加到緩存中:
var itemsInCache= HttpContext.Current.Cache.GetEnumerator();
while (itemsInCache.MoveNext())
{
HttpContext.Current.Cache.Remove(enumerator.Key);
}
其中是enumerator.Key集的值? – SeanKPS 2017-07-05 14:42:42
我不確定您希望實現的確切方法留下這個。但有幾種方法,一種方式是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");
}
}
這將成功地回收您的應用程序池。這將清除緩存。你有幾個選擇。請注意,雖然這會清除緩存,但它也會終止存在的任何會話。
希望這會有所幫助。
迭代有一個問題:它不是線程安全的。 如果您正在迭代,並且從另一個線程訪問緩存,則可能會出現錯誤。 這個概率很低,但是對於高負載應用來說這是一個問題。僅供參考,一些緩存實現甚至不提供迭代方法。另外,如果您正在清除緩存項目,則不需要清除應用程序域的每個部分的所有內容,而只是與您相關的內容。
當我遇到這個問題時,我通過爲我的所有緩存條目添加自定義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
或者只是使用「鎖定(HttpContext.Current.Cache)」使其線程安全。 – Undercover1989 2017-07-20 09:42:56
System.Web.HttpRuntime.UnloadAppDomain() - 重新啓動網絡應用程序,清除高速緩存,重置的CSS/JS束
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-11 11:32:57
我想嘗試在IIS中重新啓動站點,或者可能回收應用程序池。否則,您可能會公開一個手動刪除緩存中所有內容的頁面。 – millimoose 2013-05-13 22:28:35
@xarzu在什麼情況下你想清除緩存? – 2014-12-06 21:32:35