Skip to content

Instantly share code, notes, and snippets.

@paulvictor
Created August 30, 2012 07:53
Show Gist options
  • Save paulvictor/3523883 to your computer and use it in GitHub Desktop.
Save paulvictor/3523883 to your computer and use it in GitHub Desktop.
Ruby script for a Redis zpop
require 'redis'
get_and_rem_script = <<EOF
local resp = redis.call('zrangebyscore', KEYS[1], '-inf', '+inf', 'LIMIT', '0', '1');
if (resp[1] ~= nil) then
local val = resp[# resp]
redis.call('zrem', KEYS[1], val)
return val
else
return false
end
EOF
r = Redis.new(:port => 6379)
r.del('test')
r.zadd('test', [0, 'foo'], [1, 'bar'])
puts r.eval(get_and_rem_script, [:test1])
puts r.zrange('test', '0', '10')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment