python连redis测试





python 版本 3.x
执行环境需要安装redis模块: pip install redis

python连redis测试_第1张图片

 

 

执行脚本前,有redis-cli中查询key值:

 

 执行脚本:

python连redis测试_第2张图片

python连redis测试_第3张图片

 


****************************** redis的乐观锁 脚本如下 *****************************
import redis
def key_for(user_id):
return "account_{}".format(user_id)

def double_account(client, user_id):
key = key_for(user_id)
while True:
client.watch(key)
value = int(client.get(key))
value *=2
pipe = client.pipeline(transaction=True)
pipe.multi()
pipe.set(key,value)
try:
pipe.execute()
break
except redis.WatchError:
continue
return int(client.get(key))

if __name__ == "__main__":
client = redis.StrictRedis(host="192.168.1.100", port = 7000)
user_id = "abc"
client.setnx(key_for(user_id),5)
print (double_account(client,user_id))

转载于:https://www.cnblogs.com/linlianhuan/p/9794395.html

你可能感兴趣的:(python连redis测试)