Skip to content

Instantly share code, notes, and snippets.

@z4r
Last active October 30, 2017 16:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save z4r/6765636 to your computer and use it in GitHub Desktop.
Save z4r/6765636 to your computer and use it in GitHub Desktop.
ZRANGEBYSCORESTORE
redis.call('ZUNIONSTORE', KEYS[1], 1, KEYS[2])
local lbound, ubound = ARGV[1], ARGV[2]
if lbound ~= '-inf' then
lbound = lbound:sub(1, 1) == '(' and lbound:sub(2) or '('..lbound
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', lbound)
end
if ubound ~= '+inf' then
ubound = ubound:sub(1, 1) == '(' and ubound:sub(2) or '('..ubound
redis.call('ZREMRANGEBYSCORE', KEYS[1], ubound, '+inf')
end
import redis
from functools import partial
from itertools import product
def ztest(zrangebyscorestore, dest, source, lbound, ubound):
zrangebyscorestore(keys=[dest, source], args=[lbound, ubound])
got = client.zrangebyscore(dest, '-inf', '+inf', withscores=True)
expected = client.zrangebyscore(source, lbound, ubound, withscores=True)
assert got == expected, '{0} != {1}'.format(got, expected)
client = redis.StrictRedis(db=8)
client.script_flush()
client.flushdb()
client.zadd('SOURCE', **dict([('p{0}'.format(i), i) for i in range(5)]))
with open('zrangebyscorestore.lua') as fp:
test = partial(ztest, client.register_script(fp.read()), 'DEST', 'SOURCE')
for lbound, ubound in product(['-inf', 1, '(1'], [3, '(3', '+inf']):
test(lbound, ubound)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment