Redis实现访问次数限流,这有难点吗?
点击上方蓝字“设为星标”
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 currentcurrent = redis.call("incr",KEYS[1])if current == 1 thenredis.call("expire",KEYS[1],1)endif current > ARGV[1]return 1endreturn 0
 
评论
