Created
February 16, 2013 20:11
-
-
Save mrluanma/4968518 to your computer and use it in GitHub Desktop.
Append unique element to a Redis list atomic-ly.
This file contains 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
import redis | |
import random | |
r = redis.StrictRedis() | |
rpush_uniquely_lua = """ | |
local is_member = redis.call('SISMEMBER', KEYS[1], ARGV[1]) | |
if (is_member == 0) then | |
redis.call('SADD', KEYS[1], ARGV[1]) | |
redis.call('RPUSH', KEYS[2], ARGV[1]) | |
return 1 | |
end | |
return 0""" | |
rpush_uniquely = r.register_script(rpush_uniquely_lua) | |
def append_uniquely(x): | |
return rpush_uniquely(keys=['xxx:set', 'xxx:list'], args=[x]) | |
def main(): | |
for i in range(1000): | |
append_uniquely(random.randint(1, 10)) | |
print r.lrange('xxx:list', 0, -1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
怎么动态更改本机的sock地址啊? 就是说我的机器上有3个IP, 我想用其中一个和renren链接, 怎么改变啊?