簡體   English   中英

ASP.Net緩存在IIS 6.0下不起作用

[英]ASP.Net cache don't work under IIS 6.0

我有一個帶有簡單緩存幫助器的ASP.Net應用程序。 在VS Web服務器下,它可以正常工作。 在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);
        }
    }

也許從技術上講沒有錯。 高速緩存並不意味着在持久化對象后即會返回任何對象。

如果您的站點正在緩存很多項目,並且緩存不夠大,則它可能一直在尋找要從緩存中刪除的對象。 在這種情況下,有時僅剛剛緩存的對象可能是要刪除的良好候選對象。 如果您有100個對象的空間,並且緩存中充滿了至少被訪問過一次的項目,那么它可能永遠不會緩存“從未被訪問過”的新對象。 實際上確實是在奇怪的情況下發生的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM