Skip to content

Instantly share code, notes, and snippets.

@mrluanma
Created February 16, 2013 20:11
Show Gist options
  • Save mrluanma/4968518 to your computer and use it in GitHub Desktop.
Save mrluanma/4968518 to your computer and use it in GitHub Desktop.
Append unique element to a Redis list atomic-ly.
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()
@MartinXu
Copy link

怎么动态更改本机的sock地址啊? 就是说我的机器上有3个IP, 我想用其中一个和renren链接, 怎么改变啊?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment