pyton之redis加有效时间
stop_server_key = "server_stop"
def lock(redis, key, value, expire):
return redis.register_script(
'''
if redis.call("get", KEYS[1]) == 0 then
redis.call("setnx", KEYS[1], ARGV[1])
redis.call("expire", KEYS[1], ARGV[2])
return 1
else
return 0
end
'''
)(keys=[key], args=[value, expire])
lock(redis, stop_server_key, 1, 3)
简单点
r.set(self.stop_server_key, 1,ex=3600,nx=True)