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));
是不是 这种 缓存形势 不稳定 容易 内存回收
可能是设置的时间数值超过规定,最好重新看看文档怎么说