Created
February 1, 2016 09:52
-
-
Save adrienmo/9ce800cac1a65746d95e to your computer and use it in GitHub Desktop.
redis script to take atomically N elements from a redis sorted set according to score and limit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This script allow you to perform the following hypothetical operation in a transaction : | |
-- | |
-- ZRANGEBYSCORE key min max LIMIT 0 count | |
-- ZREMRANGEBYSCORE key min max LIMIT 0 count | |
-- | |
-- LIMIT on ZREMRANGEBYSCORE is not supported at the moment, this script resolve it | |
local keys = redis.call('zcount', KEYS[1], ARGV[1], ARGV[2]); | |
local count = math.min(keys, ARGV[3]); | |
if count == 0 then | |
return {}; | |
else | |
local result = redis.call('zrange', KEYS[1], 0, count - 1); | |
redis.call('zremrangebyrank', KEYS[1], 0, count - 1); | |
return result; | |
end | |
-- This script can be used in the following way : | |
-- | |
-- EVAL "SCRIPT" 1 key min max limit | |
-- | |
-- e.g. : EVAL "SCRIPT" 1 mykey -inf 1454320319 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment