2014-01-31 38 views
0

我有一種情況,在我們的應用中,有很多內容可以放在OutputCache中,但任何用戶在任何時候都可以刷新此內容。當任何用戶發起刷新操作時,我需要做的是完全清除緩存。如何找出OutputCache項目的名稱?

我認爲它會很簡單,HttpResponse有一個方法RemoveFromOutputCache。然而這個方法需要一個字符串,它是已經被緩存的項目的名稱。看起來好像沒有任何簡單的方法可以獲得已緩存的項目名稱列表。

因此,我已經重寫了OutputCacheAttribute下面的類:

public class HierarchyOutputCache : OutputCacheAttribute 
{ 
    public static readonly ConcurrentBag<string> CachedPages = new ConcurrentBag<string>(); 

    public HierarchyOutputCache(params string[] varyParameters) 
    { 
     Location = OutputCacheLocation.Server; 
     Duration = int.MaxValue; 
     VaryByParam = string.Join(";", varyParameters); 
    } 

    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     base.OnActionExecuted(filterContext); 
     CachedPages.Add(filterContext.HttpContext.Request.RawUrl); 
    } 
} 

然後,當某個動作在我的申請被打,我可以做到以下幾點:

HierarchyOutputCache.CachedPages。的ForEach(Response.RemoveOutputCacheItem);

然而,Response.RemoveOutputCacheItem不工作,我以爲是因爲我傳遞一個不正確的字符串與RawUrl我在我的屬性保管。

我無法找到任何有關此名稱的信息,任何人都可以幫忙嗎?

回答

1

我很容易地解決了這個在我的HierarchyOutputCache類使用下列項目名稱:

public override void OnActionExecuted(ActionExecutedContext filterContext) 
{ 
    base.OnActionExecuted(filterContext); 
    CachedPages.Add(filterContext.HttpContext.Request.CurrentExecutionFilePath); 
} 

這意味着我呼籲HierarchyOutputCache.CachedPages.ForEach(Response.RemoveOutputCacheItem)預期一樣!