首页 新闻 会员 周边

c# System.Runtime.Caching 我设置很长的过期时间 但是 最后却 几分钟 就过期了

0
悬赏园豆:15 [待解决问题]

c# System.Runtime.Caching 我设置很长的过期时间 但是 最后却 几分钟 就过期了

/// <summary>
/// Get cache(HttpContext.Current.Cache) data
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="callback">若無對應 cache data,執行 callback取得資料,並寫入 cache</param>
/// <param name="absoluteExpiration"></param>
/// <param name="forceRefresh">是否清除Cache,強制重查</param>
/// <param name="returnCopy">是否回傳複本</param>
/// <returns></returns>
public static T GetMemoryCacheData<T>(string key, Func<T> callback, TimeSpan absoluteExpiration, bool forceRefresh = false, bool returnCopy = true) where T : class
{
//return callback();// 停用 cache

        var cache = System.Runtime.Caching.MemoryCache.Default;
        string cacheKey = BuildCacheKey(key);
        lock (GetMemoryCacheLockObject(cacheKey))
        {
            T result = cache[cacheKey] as T;

            if (result != null && forceRefresh)
            {// 是否清除Cache,強制重查
                cache.Remove(cacheKey);
                result = null;
            }

            if (result == null)
            {
                result = callback();
                if (result != null)
                {
                    cache.Add(
                        cacheKey,
                        result,
                        new System.Runtime.Caching.CacheItemPolicy()
                        {
                            AbsoluteExpiration = DateTimeOffset.UtcNow.Add(absoluteExpiration)
                        }
                    );
                    //logger.Debug("insert cache: {0}", cacheKey);
                }
            }
            //else
            //{
            //    logger.Debug("cache hit: {0}", cacheKey);
            //}
            return (returnCopy && result != null) ? result.CloneByJson() : result;
        }
    }

List<User_SavedSearch> list = BigZataCache.GetMemoryCacheData(cacheName, () =>
{
return userSavedSearchRepository.GetAll(Account, UserId, PageName).ToList();
}, new TimeSpan(24, 0, 0));

是不是 这种 缓存形势 不稳定 容易 内存回收

程序员编程日记的主页 程序员编程日记 | 初学一级 | 园豆:6
提问于:2019-08-11 02:04
< >
分享
所有回答(1)
0

可能是设置的时间数值超过规定,最好重新看看文档怎么说

PER10 | 园豆:8 (初学一级) | 2021-01-02 22:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册