Ejemplo n.º 1
0
 /// <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));
     }
 }
Ejemplo n.º 2
0
        /// <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);
        }