using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; using System.Text; using System.Threading.Tasks; namespace Common { public static class MemoryCacheUtil { private static readonly Object _locker = new object(), _locker2 = new object(); /// /// 取缓存项,如果不存在则返回空 /// /// /// /// public static T GetCacheItem(String key) { try { return (T)MemoryCache.Default[key]; } catch { return default(T); } } /// /// 是否包含指定键的缓存项 /// /// /// public static bool Contains(string key) { return MemoryCache.Default.Contains(key); } /// /// 取缓存项,如果不存在则新增缓存项 /// /// /// /// /// /// /// public static T GetOrAddCacheItem(String key, Func cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null) { if (String.IsNullOrWhiteSpace(key)) throw new ArgumentException("Invalid cache key"); if (cachePopulate == null) throw new ArgumentNullException("cachePopulate"); if (slidingExpiration == null && absoluteExpiration == null) throw new ArgumentException("Either a sliding expiration or absolute must be provided"); if (MemoryCache.Default[key] == null) { lock (_locker) { if (MemoryCache.Default[key] == null) { T cacheValue = cachePopulate(); if (!typeof(T).IsValueType && ((object)cacheValue) == null) //如果是引用类型且为NULL则不存缓存 { return cacheValue; } var item = new CacheItem(key, cacheValue); var policy = CreatePolicy(slidingExpiration, absoluteExpiration); MemoryCache.Default.Add(item, policy); } } } return (T)MemoryCache.Default[key]; } /// /// 取缓存项,如果不存在则新增缓存项 /// /// /// /// /// /// public static T GetOrAddCacheItem(String key, Func cachePopulate, string dependencyFilePath) { if (String.IsNullOrWhiteSpace(key)) throw new ArgumentException("Invalid cache key"); if (cachePopulate == null) throw new ArgumentNullException("cachePopulate"); if (MemoryCache.Default[key] == null) { lock (_locker2) { if (MemoryCache.Default[key] == null) { T cacheValue = cachePopulate(); if (!typeof(T).IsValueType && ((object)cacheValue) == null) //如果是引用类型且为NULL则不存缓存 { return cacheValue; } var item = new CacheItem(key, cacheValue); var policy = CreatePolicy(dependencyFilePath); MemoryCache.Default.Add(item, policy); } } } return (T)MemoryCache.Default[key]; } /// /// 移除指定键的缓存项 /// /// public static void RemoveCacheItem(string key) { try { MemoryCache.Default.Remove(key); } catch { } } private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration) { var policy = new CacheItemPolicy(); if (absoluteExpiration.HasValue) { policy.AbsoluteExpiration = absoluteExpiration.Value; } else if (slidingExpiration.HasValue) { policy.SlidingExpiration = slidingExpiration.Value; } policy.Priority = CacheItemPriority.Default; return policy; } private static CacheItemPolicy CreatePolicy(string filePath) { CacheItemPolicy policy = new CacheItemPolicy(); policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List() { filePath })); policy.Priority = CacheItemPriority.Default; return policy; } } }