/// <summary>
        /// 设置锁
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="seconds"></param>
        /// <returns></returns>
        public static bool TryLock(int tenantId, string key, string value, int seconds = 20)
        {
            //申请成功标志
            bool success = false;

            try
            {
                using (var redis = new RedisNativeProviderV2(RedisCacheKeyspace, tenantId))
                {
                    //redis中申请key-value
                    success = redis.Set(key, StringToBytes(value), Beisen.RedisV2.Enums.SetExpireType.EX, seconds, Beisen.RedisV2.Enums.SetKeyType.NX);
                }
            }
            catch (Exception ex)
            {
                //进行创建异常的操作
                LogHelper.Instance.Error($"在redis创建[{key}]:[{value}]异常", ex);
                success = false;
                throw ex;
            }
            finally
            {
                //进行创建成功或失败的操作
                LogHelper.Instance.Debug($"在redis创建[{key}]:[{value}]{(success ? "成功" : "失败")}");
            }

            return(success);
        }
        /// <summary>
        /// 查询锁
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private static bool RedisExist(int tenantId, string key)
        {
            //查询成功标志
            bool success = false;

            try
            {
                using (var redis = new RedisNativeProviderV2(RedisCacheKeyspace, tenantId))
                {
                    //在redis查询
                    success = redis.Exists(key);
                }
            }
            catch (Exception ex)
            {
                //进行查询异常操作
                LogHelper.Instance.Error($"在redis查询[{key}]异常", ex);
                success = false;
                //抛出异常
                throw new Exception(key, ex);
            }
            finally
            {
                //进行查询成功或失败操作
                LogHelper.Instance.Debug($"在redis查询[{key}]{(success ? "存在" : "不存在")}");
            }

            return(success);
        }
        public static bool SetHRedis(int tenantId, string key, string field, string value, long timespan = 0)
        {
            bool result = false;

            try
            {
                using (var redis = new RedisNativeProviderV2(RedisCacheKeyspace, tenantId))
                {
                    //在redis查询
                    byte[] fieldByte = Encoding.Default.GetBytes(field);
                    byte[] valueByte = Encoding.Default.GetBytes(value);
                    result = redis.HSet(key, fieldByte, valueByte);
                    if (timespan > 0)
                    {
                        redis.ExpireAt(key, timespan);
                    }
                }
            }
            catch (Exception ex)
            {
                //进行查询异常操作
                LogHelper.Instance.Error($"在redis查询[{key}]异常", ex);
                //抛出异常
                throw new Exception(key, ex);
            }
            return(true);
        }
Esempio n. 4
0
 public static int Remove(int tenantId, string key)
 {
     using (var redis = new RedisNativeProviderV2(REDIS_CACHE_KEYSPACE, tenantId))
     {
         return(redis.Del(key));
     }
 }
        /// <summary>
        /// 解锁
        /// </summary>
        /// <param name="tenantId"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool UnLock(int tenantId, string key, string value)
        {
            //删除成功标志
            bool success = false;
            //异常出现次数计数
            int exceptionTimes = 0;

            while (!success)
            {
                //进行解锁尝试
                try
                {
                    //判断锁是否存在(且为自身所加锁)
                    if (RedisExist(tenantId, key))
                    {
                        using (var redis = new RedisNativeProviderV2(RedisCacheKeyspace, tenantId))
                        {
                            //redis中删除key
                            int affected = redis.Del(key);

                            //影响行数大于0,则删除成功
                            if (affected > 0)
                            {
                                //进行key-value删除成功操作
                                LogHelper.Instance.Debug($"在redis删除[{key}]:[{value}]成功,共{affected}条");
                                success = true;
                            }
                            else
                            {
                                throw new Exception(key, new Exception("删除操作影响0条,删除失败"));
                            }
                        }
                    }
                    else
                    {
                        //进行key-value不存在操作
                        LogHelper.Instance.Debug($"在redis删除[{key}]:[{value}]成功,记录不存在,无需删除");
                        success = true;
                    }
                }
                catch (Exception ex)
                {
                    //进行删除异常的操作
                    exceptionTimes++;
                    LogHelper.Instance.Error($"在redis删除[{key}]:[{value}]异常", ex);
                    success = false;

                    //异常次数超出限制,抛出异常
                    if (exceptionTimes >= 3)
                    {
                        throw new Exception(key, ex);
                    }
                }
            }
            return(success);
        }
Esempio n. 6
0
 /// <summary>
 /// 删除Redis
 /// </summary>
 /// <param name="key"></param>
 public static void DelRedis(string key)
 {
     try
     {
         using (var redis = new RedisNativeProviderV2(_KeySpace, _TenantId))
         {
             redis.Del(key);
         }
     }
     catch (Exception ex)
     {
         AppConnectLogHelper.Error(string.Format("GetRedis error:key={0}", key), ex);
     }
 }
Esempio n. 7
0
 /// <summary>
 /// 是否存在
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public static bool IsExist(string key)
 {
     try
     {
         using (var redis = new RedisNativeProviderV2(_KeySpace, _TenantId))
         {
             return(redis.Exists(key));
         }
     }
     catch (Exception ex)
     {
         AppConnectLogHelper.Error(string.Format("RedisIsExist error:key={0}", key), ex);
     }
     return(false);
 }
Esempio n. 8
0
 /// <summary>
 /// 写入Redis
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 public static void SetRedis(string key, string value)
 {
     try
     {
         using (var redis = new RedisNativeProviderV2(_KeySpace, _TenantId))
         {
             var bytes = Encoding.UTF8.GetBytes(value);
             redis.Set(key, bytes);
         }
     }
     catch (Exception ex)
     {
         AppConnectLogHelper.Error(string.Format("SetRedis error:key={0},value={1}", key, value), ex);
     }
 }
Esempio n. 9
0
        public static T Get <T>(int tenantId, string key)
        {
            T result = default(T);

            using (var redis = new RedisNativeProviderV2(REDIS_CACHE_KEYSPACE, tenantId))
            {
                var value = redis.Get(key);
                if (value != null)
                {
                    result = JsonConvert.DeserializeObject <T>(BytesToString(value));
                }
            }

            return(result);
        }
Esempio n. 10
0
 public static void Add(int tenantId, string key, string value, DateTime?expire = null)
 {
     using (var redis = new RedisNativeProviderV2(REDIS_CACHE_KEYSPACE, tenantId))
     {
         var serializedValue = JsonConvert.SerializeObject(value);
         if (expire.HasValue)
         {
             redis.SetEx(key, expire.Value.Second, StringToBytes(serializedValue));
         }
         else
         {
             redis.Set(key, StringToBytes(serializedValue));
         }
     }
 }
 public static List <string> SMembers(int tenantId, string key)
 {
     try
     {
         using (var redis = new RedisNativeProviderV2(RedisCacheKeyspace, tenantId))
         {
             return(ByteArrayToList(redis.SMembers(key)));
         }
     }
     catch (Exception ex)
     {
         LogHelper.Instance.Error($"在redis-ScanMembers查询[{key}]异常", ex);
         throw;
     }
     throw new NotImplementedException();
 }
 public static int DelHRedis(int tenantId, string key, string field)
 {
     try
     {
         using (var redis = new RedisNativeProviderV2(RedisCacheKeyspace, tenantId))
         {
             byte[] fieldBytes = Encoding.Default.GetBytes(field);
             return(redis.HDel(key, fieldBytes));
         }
     }
     catch (Exception ex)
     {
         LogHelper.Instance.Error($"在redis-Bytes删除[{key}]异常,Field:{field}", ex);
         return(0);
     }
 }
 public static void SAdd(int tenantId, string key, string value)
 {
     try
     {
         using (var redis = new RedisNativeProviderV2(RedisCacheKeyspace, tenantId))
         {
             byte[] members = Encoding.Default.GetBytes(value);
             redis.SAdd(key, members);
         }
     }
     catch (Exception ex)
     {
         LogHelper.Instance.Error($"在redis-SAdd创建[{key}]:[{value}]异常", ex);
         throw;
     }
 }
Esempio n. 14
0
 /// <summary>
 /// 获取
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string GetRedis(string key)
 {
     try
     {
         using (var redis = new RedisNativeProviderV2(_KeySpace, _TenantId))
         {
             var value = redis.Get(key);
             if (value != null && value.Length != 0)
             {
                 return(Encoding.UTF8.GetString(value));
             }
         }
     }
     catch (Exception ex)
     {
         AppConnectLogHelper.Error(string.Format("GetRedis error:key={0}", key), ex);
     }
     return(null);
 }
 public static byte[][] GetHRedisValueByKey(int tenantId, string key)
 {
     byte[][] result = new byte[][] { };
     try
     {
         using (var redis = new RedisNativeProviderV2(RedisCacheKeyspace, tenantId))
         {
             //在redis查询
             result = redis.HGetAll(key);
         }
     }
     catch (Exception ex)
     {
         //进行查询异常操作
         LogHelper.Instance.Error($"在redis查询[{key}]异常", ex);
         //抛出异常
         throw new Exception(key, ex);
     }
     return(result);
 }
 public static string GetHRedis(int tenantId, string key, string field)
 {
     byte[] result = new byte[] { };
     try
     {
         using (var redis = new RedisNativeProviderV2(RedisCacheKeyspace, tenantId))
         {
             //在redis查询
             byte[] fieldByte = Encoding.Default.GetBytes(field);
             result = redis.HGet(key, fieldByte);
         }
     }
     catch (Exception ex)
     {
         //进行查询异常操作
         LogHelper.Instance.Error($"在redis查询[{key}]异常", ex);
         //抛出异常
         throw new Exception(key, ex);
     }
     return(BytesToString(result));
 }