tnblog
首页
视频
资源
登录

Rdis间隔限制

3943人阅读 2020/3/31 16:10 总访问:437368 评论:0 收藏:0 手机
分类: ASP.NET
  1. 1.public static bool SpanLimit(string key, long seconds)
  2.         {
  3.             string redisKey = $"{key}_redis";
  4.             if (Redis.StringHelper.StringSetIfNotExists<int>(redisKey, 1))
  5.             {
  6.                 Redis.KeyHelper.KeyExpire(redisKey, TimeSpan.FromSeconds(seconds));
  7.                 return false;
  8.             }
  9.             else
  10.             {
  11.                 return true;
  12.             }
  13.         }
  14. 2.日调用次数限制
  15. public static bool CallDayTimesLimit(string key, int limitCount)
  16.         {
  17.             key = $"{key}_{DateTime.Now.ToString("yy-MM-dd")}";
  18.             int count = CommonService.Redis.StringHelper.StringGet<int>(key);
  19.             if (count > limitCount)
  20.             {
  21.                 return true;
  22.             }
  23.             count = count + 1;
  24.             CommonService.Redis.StringHelper.StringSet<int>(key, count, CommonHelper.GetDayTimeLeft().Add(TimeSpan.FromMinutes(10)));
  25.             return false;
  26.         }
  27. 3.获取日调用次数限制
  28.  public static int GetDayTimesLimit(string key)
  29.         {
  30.             key = $"{key}_{DateTime.Now.ToString("yy-MM-dd")}";
  31.             int count = CommonService.Redis.StringHelper.StringGet<int>(key);
  32.             return count;
  33.         }
  34. public static int SubTimes(string key, int num = 1)
  35.         {
  36.             key = $"{key}_{DateTime.Now.ToString("yy-MM-dd")}";
  37.             int count = CommonService.Redis.StringHelper.StringGet<int>(key);
  38.             count = count - num;
  39.             CommonService.Redis.StringHelper.StringSet<int>(key, count, CommonHelper.GetDayTimeLeft().Add(TimeSpan.FromMinutes(10)));
  40.             return count;
  41.         }
  42.         public static int AddTimes(string key, int num = 1)
  43.         {
  44.             key = $"{key}_{DateTime.Now.ToString("yy-MM-dd")}";
  45.             int count = CommonService.Redis.StringHelper.StringGet<int>(key);
  46.             count = count + num;
  47.             CommonService.Redis.StringHelper.StringSet<int>(key, count, CommonHelper.GetDayTimeLeft().Add(TimeSpan.FromMinutes(10)));
  48.             return count;
  49.         }


评价
当你知道迷惑时,并不可怜,当你不知道迷惑时,才是最可怜的。
排名
11
文章
201
粉丝
10
评论
13
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术