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);
}
반복에 문제가 있습니다. 스레드가 안전하지 않습니다.반복할 때 다른 스레드에서 캐시에 액세스하면 오류가 발생할 수 있습니다.이는 확률은 낮지만 고부하 애플리케이션의 문제입니다.참고로, 일부 캐시 구현은 반복 방법을 제공하지도 않습니다.
또한 캐시 항목을 지우는 경우 앱 도메인의 모든 부분에서 모든 항목을 지우는 것이 아니라 자신과 관련된 항목만 지우는 것입니다.
이 문제가 발생했을 때 모든 캐시 항목에 사용자 지정 캐시 종속성을 추가하여 문제를 해결했습니다.
캐시 종속성은 다음과 같이 정의됩니다.
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
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);
}
이 작업을 수행할 정확한 방법을 모르겠습니다.하지만 몇 가지 방법이 있습니다. 한 가지 방법은 조르지오 미나디가 올린 질문입니다.
다른 선택사항은 다음과 같습니다.
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");
}
}
응용 프로그램 풀을 성공적으로 재활용합니다.그러면 캐시가 지워집니다.선택의 여지가 몇 가지 있습니다.이렇게 하면 캐시가 지워지지만 기존 세션도 종료됩니다.
이게 도움이 되길 바랍니다.
시스템. 웹.Http 런타임.UnloadAppDomain() - 웹 애플리케이션을 다시 시작하고, 캐시를 지우고, 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")
언급URL : https://stackoverflow.com/questions/16532146/how-do-i-clear-the-server-cache-in-asp-net
'sourcetip' 카테고리의 다른 글
목록(String) 또는 배열 또는 배열 목록 (0) | 2023.05.23 |
---|---|
노드JS: base64 인코딩된 문자열을 다시 이진으로 디코딩하는 방법은 무엇입니까? (0) | 2023.05.18 |
jQuery를 사용하여 div에서 선택한 확인란 목록 가져오기 (0) | 2023.05.18 |
XElement와 XDocument의 차이점은 무엇입니까? (0) | 2023.05.18 |
이클립스 작업 공간에서 프로젝트 목록은 어디에 저장됩니까? (0) | 2023.05.18 |