![](/img/trans.png)
[英]How to clear the cache of an asp.net application from another asp.net application on the same server?
[英]How do I clear the server cache in asp.net?
如何清除asp.net中的服務器緩存? 我發現緩存有兩種。 有瀏覽器緩存和服務器緩存。 我已經進行了一些搜索,但是還沒有找到一個清晰的分步指南,以使用asp.net(或不使用)清除服務器緩存。
(更新)我剛剛了解到,其背后的代碼在VB-Visual Basic(點網)中。
您可以遍歷所有緩存項,然后將其一一刪除:
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);
}
迭代存在一個問題:它不是線程安全的。 如果要迭代,並且從另一個線程訪問了緩存,則可能會遇到錯誤。 發生這種情況的可能性很低,但這是高負載應用程序中的一個問題。 僅供參考,某些緩存實現甚至不提供迭代方法。
另外,如果要清除緩存項,則不想清除應用程序域的每個部分中的所有內容,而只清除與您相關的內容。
遇到此問題時,我通過向所有緩存條目添加自定義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
我不確定您要完成此操作的確切方法。 但是有幾種方法,一種方法是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");
}
}
這將成功回收您的應用程序池。 這將清除緩存。 您有幾種選擇。 請注意,盡管這將清除緩存,但也會終止所有存在的會話。
希望這會有所幫助。
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]);
}
您需要刪除添加到緩存中的項目:
var itemsInCache= HttpContext.Current.Cache.GetEnumerator();
while (itemsInCache.MoveNext())
{
HttpContext.Current.Cache.Remove(enumerator.Key);
}
System.Web.HttpRuntime.UnloadAppDomain()-重新啟動Web應用程序,清除緩存,重置CSS / JS包
在頁面加載事件..即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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.