crossoverJie / distributed-redis-tool

🔒A simple distributed tools based on Redis.
https://crossoverjie.top/tags/Distributed-Lock/
MIT License
613 stars 305 forks source link

为什么每次incr都要调用 expire #18

Open tianleios opened 5 years ago

tianleios commented 5 years ago

else -- 没有达到阈值 value + 1 redis.call("INCRBY", key, 1) redis.call("EXPIRE", key, 2) return curentLimit + 1 end

majintao commented 5 years ago

脚本有问题,应该这么写 local key = KEYS[1] -- 限流大小 local limit = tonumber(ARGV[1])

-- 获取当前流量大小 local curentLimit = tonumber(redis.call('get', key) or "0")

if curentLimit + 1 > limit then -- 达到限流大小 返回 return 0; else -- 没有达到阈值 value + 1 redis.call("INCRBY", key, 1) if curentLimit == 0 then redis.call("EXPIRE", key, 1) return curentLimit + 1 end