分类:
ASP.NET
1.public static bool SpanLimit(string key, long seconds) { string redisKey = $"{key}_redis"; if (Redis.StringHelper.StringSetIfNotExists<int>(redisKey, 1)) { Redis.KeyHelper.KeyExpire(redisKey, TimeSpan.FromSeconds(seconds)); return false; } else { return true; } } 2.日调用次数限制 public static bool CallDayTimesLimit(string key, int limitCount) { key = $"{key}_{DateTime.Now.ToString("yy-MM-dd")}"; int count = CommonService.Redis.StringHelper.StringGet<int>(key); if (count > limitCount) { return true; } count = count + 1; CommonService.Redis.StringHelper.StringSet<int>(key, count, CommonHelper.GetDayTimeLeft().Add(TimeSpan.FromMinutes(10))); return false; } 3.获取日调用次数限制 public static int GetDayTimesLimit(string key) { key = $"{key}_{DateTime.Now.ToString("yy-MM-dd")}"; int count = CommonService.Redis.StringHelper.StringGet<int>(key); return count; } public static int SubTimes(string key, int num = 1) { key = $"{key}_{DateTime.Now.ToString("yy-MM-dd")}"; int count = CommonService.Redis.StringHelper.StringGet<int>(key); count = count - num; CommonService.Redis.StringHelper.StringSet<int>(key, count, CommonHelper.GetDayTimeLeft().Add(TimeSpan.FromMinutes(10))); return count; } public static int AddTimes(string key, int num = 1) { key = $"{key}_{DateTime.Now.ToString("yy-MM-dd")}"; int count = CommonService.Redis.StringHelper.StringGet<int>(key); count = count + num; CommonService.Redis.StringHelper.StringSet<int>(key, count, CommonHelper.GetDayTimeLeft().Add(TimeSpan.FromMinutes(10))); return count; }
评价
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术