Skip to content

Instantly share code, notes, and snippets.

@nrk
Created December 28, 2010 15:10
Show Gist options
  • Select an option

  • Save nrk/757306 to your computer and use it in GitHub Desktop.

Select an option

Save nrk/757306 to your computer and use it in GitHub Desktop.
Implementation of ZPOP with redis-lua + CAS transactions
require 'luarocks.require'
require 'redis'
-- See "WATCH explained" from http://redis.io/topics/transactions
--[[
./redis-cli ZADD zset 1 a
./redis-cli ZADD zset 2 b
./redis-cli ZADD zset 3 c
]]
local redis = Redis.connect()
function zpop(client, key)
assert(type(key) == 'string', 'key must be a string')
local element
local options = { watch = key, cas = true }
local tx_reply = client:transaction(options, function(t)
element = t:zrange(key, 0, 0)[1]
if element then
t:multi()
t:zrem(key, element)
end
end)
return element
end
local value = zpop(redis, 'zset')
print(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment