Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Lua script that can be embedded in Redis server for adding an element to a KMinHash set backed by a Redis sorted set
local count = redis.call('ZCARD', KEYS[1]);
if count == tonumber(ARGV[1]) then
local max_score = redis.call('ZRANGE', KEYS[1], -1, -1, 'WITHSCORES')[2]
if tonumber(ARGV[2]) >= tonumber(max_score) then
return
else
redis.call('ZREMRANGEBYRANK', KEYS[1], -1, -1)
end
end
redis.call('ZADD', KEYS[1], tonumber(ARGV[2]), tonumber(ARGV[3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment