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