Redis实现访问次数限流,这有难点吗?
猿天地
共 1906字,需浏览 4分钟
·
2022-03-03 19:45
点击上方蓝字“设为星标”
long count = redisTemplate.opsForValue().increment("user:1:60");
if (count > maxLimitCount) {
throw new LimitException("访问太频繁");
}
count = redisTemplate.opsForValue().increment("user:1:600");
if (count > maxLimitCount) {
throw new LimitException("访问太频繁");
}
redisTemplate.execute(new RedisCallback
() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline();
connection.incr("user:1:60".getBytes());
connection.incr("user:1:600".getBytes());
onnection.closePipeline();
return null;
}
});
local current
current = redis.call("incr",KEYS[1])
if current == 1 then
redis.call("expire",KEYS[1],1)
end
if current > ARGV[1]
return 1
end
return 0
评论