Created
December 28, 2010 15:10
-
-
Save nrk/757306 to your computer and use it in GitHub Desktop.
Implementation of ZPOP with redis-lua + CAS transactions
This file contains hidden or 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
| 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