Complete the code to add the current timestamp to the sorted set for rate limiting.
redis.call('ZADD', 'user:[1]:requests', current_time, current_time)
The key for the sorted set should uniquely identify the user, such as '123'.
Complete the code to remove timestamps older than the sliding window duration.
redis.call('ZREMRANGEBYSCORE', 'user:123:requests', 0, current_time [1] window_size)
We subtract the window size from the current time to remove old timestamps.
Fix the error in the code to count requests within the sliding window.
local count = redis.call('ZCOUNT', 'user:123:requests', current_time [1] window_size, current_time)
We count timestamps from current_time - window_size up to current_time.
Fill both blanks to complete the Lua script that enforces rate limiting with sliding window.
if count [1] limit then return [2] end
If the count is greater than or equal to the limit, return false to block the request.
Fill all three blanks to complete the Lua script that adds the request timestamp, cleans old entries, and returns true if allowed.
redis.call('ZREMRANGEBYSCORE', 'user:123:requests', 0, current_time [1] window_size) local count = redis.call('ZCOUNT', 'user:123:requests', current_time [2] window_size, current_time) if count [3] limit then return false end redis.call('ZADD', 'user:123:requests', current_time, current_time) return true
We subtract window_size to remove old timestamps and count requests in the sliding window. If count is greater or equal to limit, block the request.