/// <summary> /// 获取缓存 /// </summary> /// <param name="cacheKey">缓存的Key</param> /// <param name="isRedis">是否为Redis</param> /// <returns></returns> public static T Get <T>(string cacheKey, bool isRedis = false) { if (isRedis && redisEnabled) { return(RedisUtil.Get <T>(cacheKey)); } else { return(MemoryUtil.Get <T>(cacheKey)); } }
/// <summary> /// 包含 /// </summary> /// <param name="cacheKey">缓存的Key</param> /// <param name="isRedis">是否为Redis</param> /// <returns></returns> public static bool Contains(string cacheKey, bool isRedis = false) { if (isRedis && redisEnabled) { return(RedisUtil.Contains(cacheKey)); } else { return(MemoryUtil.Contains(cacheKey)); } }
/// <summary> /// 删除缓存 /// </summary> /// <param name="pattern">正则</param> /// <param name="isRedis">是否为Redis</param> /// <returns></returns> public static void RemoveByRegex(string pattern, bool isRedis = false) { if (isRedis && redisEnabled) { RedisUtil.RemoveByRegex(pattern); } else { MemoryUtil.RemoveByRegex(pattern); } }
/// <summary> /// 删除全部缓存 /// </summary> /// <param name="isRedis">是否为Redis</param> /// <returns></returns> public static void RemoveAllCache(bool isRedis = false) { if (isRedis && redisEnabled) { RedisUtil.RemoveAll(); } else { MemoryUtil.RemoveAll(); } }
/// <summary> /// 内存缓存处理 微软 /// </summary> /// <param name="cacheKey">缓存的Key</param> /// <param name="proc">处理函数</param> /// <param name="isFromCache">是否从缓存读取</param> /// <param name="refreshCache">是否强制刷新</param> /// <param name="cacheTime"></param> /// <returns></returns> private static T MemoryCache <T>(string cacheKey, Func <T> proc, bool isFromCache = true, bool refreshCache = false, TimeSpan cacheTime = default(TimeSpan)) { T result; if (isFromCache) { //缓存 if (MemoryUtil.Get(cacheKey) != null) //判断是否有缓存 { //已经缓存 if (refreshCache)//是否强制刷新缓存 { //强制刷新 result = proc(); if (result != null) { MemoryUtil.Set(cacheKey, result, cacheTime); } } else { //不强制刷新 try { result = (T)MemoryUtil.Get(cacheKey); } catch (Exception ex) { result = proc(); LogUtil.WriteException(ex); } } } else { //未缓存 result = proc(); if (result != null) { MemoryUtil.Set(cacheKey, result, cacheTime); } } } else { result = proc(); } return(result); }
/// <summary> /// 获取缓存 /// </summary> /// <param name="cacheKey">缓存的Key</param> /// <param name="t"></param> /// <param name="cacheTime"></param> /// <param name="isRedis">是否为Redis</param> /// <returns></returns> public static bool Set <T>(string cacheKey, T t, TimeSpan cacheTime = default(TimeSpan), bool isRedis = false) { if (isRedis && redisEnabled) { if (cacheTime == default(TimeSpan)) { //设置默认Redis缓存1毫秒 cacheTime = TimeSpan.FromMilliseconds(BaseSystemInfo.RedisCacheMillisecond); } return(RedisUtil.Set <T>(cacheKey, t, cacheTime)); } else { if (cacheTime == default(TimeSpan)) { //设置默认Redis缓存1毫秒 cacheTime = TimeSpan.FromMilliseconds(BaseSystemInfo.MemoryCacheMillisecond); } MemoryUtil.Set <T>(cacheKey, t, cacheTime); return(true); } }