/// <summary>
 /// 删除指定的key
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public static bool Delete(string key)
 {
     return(SuccessOrFail(() =>
     {
         if (RedisConfigHelper.GetEnableMemoryCache())
         {
             GetCache().Remove(key);
         }
         RedisHelper.Delete(key);
     }));
 }
 /// <summary>
 /// 缓存中是否存在
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public static bool Exist(string key)
 {
     if (RedisConfigHelper.GetEnableMemoryCache())
     {
         object cache;
         if (GetCache().TryGetValue(key, out cache))
         {
             return(true);
         }
     }
     return(RedisHelper.IsExist(key));
 }
 /// <summary>
 /// 保存缓存
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="expiresencends">缓存有效时间,单位秒</param>
 /// <returns></returns>
 public static bool Set(string key, object value, int expiresencends)
 {
     IsNullOrEmptyString(key);
     return(SuccessOrFail(() =>
     {
         if (RedisConfigHelper.GetEnableMemoryCache())
         {
             GetCache().Set(key, value, TimeSpan.FromSeconds(expiresencends));
         }
         RedisHelper.Set(key, value, expiresencends);
     }));
 }
 /// <summary>
 /// 清空缓存
 /// </summary>
 /// <returns></returns>
 public static bool Clear()
 {
     return(SuccessOrFail(() =>
     {
         List <string> keys = RedisHelper.GetAllKeys();
         if (RedisConfigHelper.GetEnableMemoryCache())
         {
             foreach (var key in keys)
             {
                 cache.Remove(key);
             }
         }
         RedisHelper.DeleteAllKeys();
     }));
 }
 /// <summary>
 /// 保存缓存
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="expireTime">失效时间</param>
 /// <returns></returns>
 public static bool Set(string key, object value, DateTime expireTime)
 {
     IsNullOrEmptyString(key);
     return(SuccessOrFail(() =>
     {
         if (RedisConfigHelper.GetEnableMemoryCache())
         {
             GetCache().Set(key, value, new MemoryCacheEntryOptions()
             {
                 AbsoluteExpiration = expireTime
             });
         }
         RedisHelper.Set(key, value, expireTime);
     }));
 }
 /// <summary>
 /// 单例获取redis服务
 /// </summary>
 /// <returns></returns>
 private static IServer GetServer()
 {
     lock (redisLock)
     {
         if (_connection != null && _connection.IsConnected)
         {
             return(_connection.GetServer(_connection.GetEndPoints()[0]));
         }
         if (_connection != null)
         {
             _connection.Dispose();
         }
         _connection = ConnectionMultiplexer.Connect(RedisConfigHelper.GetRedisConnectionString());
         return(_connection.GetServer(_connection.GetEndPoints()[0]));
     }
 }
 /// <summary>
 /// 单例获取redis连接数据库
 /// </summary>
 /// <returns></returns>
 private static IDatabase GetDatabase()
 {
     lock (redisLock)
     {
         if (_connection != null && _connection.IsConnected)
         {
             return(_connection.GetDatabase());
         }
         if (_connection != null)
         {
             _connection.Dispose();
         }
         _connection = ConnectionMultiplexer.Connect(RedisConfigHelper.GetRedisConnectionString());
         return(_connection.GetDatabase());
     }
 }
 private static MemoryCache GetCache()
 {
     if (cache == null)
     {
         lock (cacheLock)
         {
             if (cache == null)
             {
                 cache = new MemoryCache(new MemoryCacheOptions()
                 {
                     //每30秒扫描一次,删除失效的缓存
                     ExpirationScanFrequency = TimeSpan.FromSeconds(RedisConfigHelper.GetMemoryCacheExpirationScanFrequency())
                 });
             }
         }
     }
     return(cache);
 }
 /// <summary>
 /// 获取缓存
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="key"></param>
 /// <returns></returns>
 public static T Get <T>(string key)
 {
     try
     {
         IsNullOrEmptyString(key);
         if (RedisConfigHelper.GetEnableMemoryCache())
         {
             T cache;
             if (GetCache().TryGetValue <T>(key, out cache))
             {
                 return(cache);
             }
         }
         return(RedisHelper.Get <T>(key));
     }
     catch (Exception ex)
     {
         return(default(T));
     }
 }