通用缓存类
1.接口
public interface ICacheProvider { /// <summary> /// 获取缓存. /// </summary> /// <param name="key">The key.</param> /// <returns></returns> T Get<T>(string key); /// <summary> /// 设置缓存. /// </summary> /// <param name="key">The key.</param> /// <param name="data">The data.</param> /// <param name="seconds">The seconds.</param> void Set<T>(string key, T data, int seconds); /// <summary> /// 是否已经设置缓存 /// </summary> /// <param name="key">key</param> /// <returns>Result</returns> bool IsSet(string key); /// <summary> /// 删除缓存. /// </summary> /// <param name="key">The key.</param> void Remove(string key); /// <summary> /// 根据键值对的模式删除缓存. /// </summary> /// <param name="pattern">The pattern.</param> void RemoveByPattern(string pattern); /// <summary> /// 清空缓存. /// </summary> void Clear(); }
2.MemeroyCache
public class MemoryCacheProvider : ICacheProvider { /// <summary> /// 获取缓存. /// </summary> /// <param name="key">缓存key.</param> /// <returns></returns> public virtual T Get<T>(string key) { return (T)MemoryCache.Default.Get(key); } /// <summary> /// 获取缓存. /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="key">缓存Key</param> /// <param name="second">缓存时间</param> /// <param name="func">当取不到缓存时,设置缓存的方法</param> /// <returns></returns> public virtual T Get<T>(string key, int second, Func<T> func) { if (key == null) { throw new ArgumentNullException("参数key不能为空"); } var setted = IsSet(key); if (setted) { return Get<T>(key); } var result = func(); if (result != null) { Set(key, result, second); } return (T)result; } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="func"></param> /// <returns></returns> public virtual T Get<T>(string key, Func<T> func) { return Get<T>(key, 60, func); } /// <summary> /// 设置缓存. /// </summary> /// <param name="key">The key.</param> /// <param name="data">The data.</param> /// <param name="seconds">The seconds.</param> public virtual void Set<T>(string key, T data, int seconds) { if (key == null) { throw new ArgumentNullException("参数key不能为空"); } if (data != null) { throw new ArgumentNullException("参数data不能为空"); } var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromSeconds(seconds); MemoryCache.Default.Add(new CacheItem(key, data), policy); } /// <summary> /// 是否已经设置缓存 /// </summary> /// <param name="key">key</param> /// <returns> /// Result /// </returns> public bool IsSet(string key) { return MemoryCache.Default.Contains(key); } /// <summary> /// 删除缓存. /// </summary> /// <param name="key">The key.</param> public virtual void Remove(string key) { MemoryCache.Default.Remove(key); } /// <summary> /// 根据键值对的模式删除缓存. /// </summary> /// <param name="pattern">The pattern.</param> public virtual void RemoveByPattern(string pattern) { foreach (string key in GetCacheKeysByPattern(pattern)) { this.Remove(key); } } /// <summary> /// 清空缓存. /// </summary> public virtual void Clear() { foreach (var item in MemoryCache.Default) { this.Remove(item.Key); } } /// <summary> /// 根据键值对的模式获取缓存. /// </summary> /// <param name="pattern"></param> /// <returns></returns> IEnumerable<string> GetCacheKeysByPattern(string pattern) { var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase); foreach (var item in MemoryCache.Default) { if (regex.IsMatch(item.Key)) { yield return item.Key; } } } }