memoryCache的使用
1 借鉴这篇文章
https://www.cnblogs.com/zuowj/p/8440902.html
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.Caching; using System.Diagnostics; namespace ConsoleApp1 { public static class MemoryCacheHelper { private static readonly object _locker = new object(); private static readonly object _locker2 = new object(); /// <summary> /// 根据key取缓存,不存在则返回null /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public static T GetCacheItem<T>(String key) { try { return (T)MemoryCache.Default[key]; } catch (Exception ex) { return default(T); } } /// <summary> /// 是否包含指定键的缓存项 /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool Contains(string key) { return MemoryCache.Default.Contains(key); } public static T GetOrAddCacheItem<T>(string key, Func<T> cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentException("Invalid cache key"); } if (cachePopulate == null) { throw new ArgumentException("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) { 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<T>(string key,Func<T> cachePopulate,string dependencyFilePath) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentException("Invalid cache key"); } if (cachePopulate == null) { throw new ArgumentException("cachePopulate"); } if (MemoryCache.Default[key]==null) { lock (_locker2) { if (MemoryCache.Default[key] == null) { T cacheValue = cachePopulate(); if (!typeof(T).IsValueType && ((object)cacheValue) == null) { return cacheValue; } var item = new CacheItem(key, cacheValue); var policy = CreatePolicy(dependencyFilePath); MemoryCache.Default.Add(item, policy); } } } return (T)MemoryCache.Default[key]; } /// <summary> /// 移除指定键的缓存项 /// </summary> /// <param name="key"></param> public static void RemoveCacheItem(string key) { try { MemoryCache.Default.Remove(key); } catch (Exception) { } } private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration) { var policy = new CacheItemPolicy(); if (slidingExpiration.HasValue) { policy.SlidingExpiration = slidingExpiration.Value; } else if (absoluteExpiration.HasValue) { policy.AbsoluteExpiration = absoluteExpiration.Value; } policy.Priority = CacheItemPriority.Default; return policy; } /// <summary> /// 缓存文件 /// </summary> /// <param name="filepath"></param> /// <returns></returns> private static CacheItemPolicy CreatePolicy(string filepath) { var policy = new CacheItemPolicy(); policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List<string>() { filepath })); policy.Priority = CacheItemPriority.Default; return policy; } } }