/// <summary> /// 根据Key,通过正则匹配从WrapCacheConfigItems里帅选出符合的缓存项目,然后通过字典缓存起来 /// </summary> /// <param name="key">根据Key获取缓存配置项</param> /// <returns>缓存配置项</returns> /// 时间:2015-12-31 15:52 /// 备注: /// <exception cref="System.Exception"></exception> public static WrapCacheConfigItem GetCurrentWrapCacheConfigItem(string key) { if (wrapCacheConfigItemDic == null) { wrapCacheConfigItemDic = new Dictionary <string, WrapCacheConfigItem>(); } if (wrapCacheConfigItemDic.ContainsKey(key)) { return(wrapCacheConfigItemDic[key]); } WrapCacheConfigItem _currentWrapCacheConfigItem = WrapCacheConfigItems.Where(i => Regex.IsMatch(ModuleName, i.CacheConfigItem.ModuleRegex, RegexOptions.IgnoreCase) && Regex.IsMatch(key, i.CacheConfigItem.KeyRegex, RegexOptions.IgnoreCase)) .OrderByDescending(i => i.CacheConfigItem.Priority).FirstOrDefault(); if (_currentWrapCacheConfigItem == null) { throw new Exception(string.Format("依据'{0}'获取缓存配置项异常!", key)); } lock (lockObj) { if (!wrapCacheConfigItemDic.ContainsKey(key)) { wrapCacheConfigItemDic.Add(key, _currentWrapCacheConfigItem); } } return(_currentWrapCacheConfigItem); }
/// <summary> /// 获取缓存 /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="key">键</param> /// <param name="acquireFactory">若缓存不存在则获取后在存储到缓存</param> /// <returns>值</returns> public static T Get <T>(string key, Func <T> acquireFactory) { WrapCacheConfigItem _cacheConfig = CacheConfigContext.GetCurrentWrapCacheConfigItem(key); ICacheProvider _cacheProvider = _cacheConfig.CacheProvider; if (_cacheProvider.IsSet(key)) { return(_cacheProvider.Get <T>(key)); } var _result = acquireFactory(); _cacheProvider.Set(key, _result, _cacheConfig.CacheConfigItem.Minitus, _cacheConfig.CacheConfigItem.IsAbsoluteExpiration); return(_result); }
/// <summary> /// 以键取值 /// </summary> /// <param name="key">键</param> /// <returns>值</returns> public static object Get(string key) { WrapCacheConfigItem _cacheConfig = CacheConfigContext.GetCurrentWrapCacheConfigItem(key); return(_cacheConfig.CacheProvider.Get(key)); }
/// <summary> /// 设置缓存 /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> public static void Set(string key, object value) { WrapCacheConfigItem _cacheConfig = CacheConfigContext.GetCurrentWrapCacheConfigItem(key); _cacheConfig.CacheProvider.Set(key, value, _cacheConfig.CacheConfigItem.Minitus, _cacheConfig.CacheConfigItem.IsAbsoluteExpiration, null); }
/// <summary> /// 移除 /// </summary> /// <param name="key">键</param> public static void Remove(string key) { WrapCacheConfigItem _cacheConfig = CacheConfigContext.GetCurrentWrapCacheConfigItem(key); _cacheConfig.CacheProvider.Remove(key); }