2010-10-15 32 views
0

我有一個帶有簡單緩存助手的ASP.Net應用程序。 在VS網絡服務器下工作正常。 在IIS 6.0緩存下不工作 - 對象,被保存在previos中,一分鐘後不會返回(沒有例外)。 什麼可能是錯的?ASP.Net緩存在IIS 6.0下不起作用

public static class CacheHelper 
    { 
     public static string Share<T>(T @object, TimeSpan period) 
     { 
      var uniqueKey = Guid.NewGuid().ToString(); 
      HttpContext.Current.Cache.Add(uniqueKey, @object, null, Cache.NoAbsoluteExpiration, 
       period, CacheItemPriority.BelowNormal, null); 
      return uniqueKey; 
     } 
     public static void ShareViaCookie<T>(string key, T @object, TimeSpan period) 
     { 
      var cachedObject = GetFromCookie<T>(key); 
      if (ReferenceEquals(cachedObject, null)) 
      { 
       var uniqueKey = Share(@object, period); 
       HttpContext.Current.Response.Cookies.Set(new HttpCookie(key, uniqueKey) 
       { 
        Expires = DateTime.Now.AddYears(1) 
       }); 
      } 
      else 
      { 
       HttpContext.Current.Cache[GetKeyFromCookie(key)] = @object; 
      } 
     } 

     public static T GetShared<T>(string key) 
     { 
      string uniqueKey = HttpContext.Current.Request.QueryString[key]; 

      return !string.IsNullOrEmpty(uniqueKey) ? (T)HttpContext.Current.Cache.Get(uniqueKey) : GetFromCookie<T>(key); 
     } 

     private static T GetFromCookie<T>(string key) 
     { 
      string uniqueKey = GetKeyFromCookie(key); 

      return !string.IsNullOrEmpty(uniqueKey) ? (T)HttpContext.Current.Cache.Get(uniqueKey) : default(T); 
     } 

     private static string GetKeyFromCookie(string key) 
     { 
      return HttpContext.Current.Request.Cookies[key] 
       .IIf(it => it != null, it => it.Value, it => null); 
     } 
    } 
+0

發表代碼片段。很難診斷而不看代碼。 – MonkeyWrench 2010-10-15 14:05:02

+2

http://stackoverflow.com/questions/1330347/asp-net-cache-object-issues-after-migration-from-iis-5-to-iis-6 – 2010-10-15 14:05:52

回答

2

也許沒有什麼是技術上的錯誤。一個緩存並不意味着任何對象在它被保存之後都會被返回。

如果您的站點正在緩存大量項目並且緩存不夠大,它可能會一直在尋找要從緩存中刪除的對象。在這種情況下,有時剛剛被緩存的對象可能是一個很好的候選對象。如果您有足夠空間容納100個對象,並且緩存中已滿的項目至少被訪問過一次,那麼它可能永遠不會緩存「從未訪問過」的新對象。這確實發生在奇怪的場合。