tnblog
首页
视频
资源
登录

Redis分布式锁

4422人阅读 2020/3/31 16:07 总访问:342065 评论:0 收藏:0 手机
分类: ASP.NET
  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.         }

评价
当你知道迷惑时,并不可怜,当你不知道迷惑时,才是最可怜的。
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术