幽梦紫曦

Rdis间隔限制

电脑版发表于:2020/3/31 16:10
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;
        }


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