幽梦紫曦

Redis分布式锁

电脑版发表于:2020/3/31 16:07
  1.  public static RedisClient Redis;

  2. static CommonService()

  3. {

  4.      Redis = new RedisClient();

  5. }

  6. public static dynamic RedisLock(string key, Func<dynamic> act, int spanSeconds = 20)

  7.         {

  8.             try

  9.             {

  10.                 using (var client = Redis.KeyHelper.GetClient())

  11.                 {

  12.                     using (client.AcquireLock(key, TimeSpan.FromSeconds(spanSeconds)))

  13.                     {

  14.                         return act();

  15.                     }

  16.                 }

  17.             }

  18.             catch (Exception ex)

  19.             {

  20.                 LogHelper.WriteError($"redis锁错误,key:{key}", ex);

  21.                 return CommonService.GetErrorResult();

  22.             }

  23.         }

  24. //日志(LogHelper)

  25. public static void WriteError(string info, Exception ex)

  26.         {

  27.             if (_logExceptionEnable)

  28.             {

  29.                 _logger.Error(ex, info);

  30.             }

  31.         }

关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢