-- sorted set key of token bucket
local key = KEYS[1];
-- Current date The format is yyyy-MM-dd
local member = ARGV[1];
-- The time stamp corresponding to the current date 0 points is in seconds
local timestamp = tonumber(ARGV[2]);
local exists = redis.call('exists', key);
if exists == 0 then
-- Create and authorize
redis.call('zadd', key, timestamp, member);
redis.call('expire', key, 7 * 24 * 60 * 60);
return 1;
else
-- Removing the authorization record seven days ago is essentially a recycling token
local sevenDaysAgo = timestamp - 7 * 24 * 60 * 60;
redis.call('zremrangebyscore', key, '-inf', sevenDaysAgo);
-- If the queue length is less than 3, then authorization is attempted
local length = redis.call('zcard', key);
if length < 3 then
local count = redis.call('zcount', key, timestamp, timestamp);
if (count == 0) then
-- No more than once a day
redis.call('zadd', key, timestamp, member);
redis.call('expire', key, 7 * 24 * 60 * 60);
return 1;
else
return 0;
end;
else
return 0;
end;
end;