【发布时间】:2018-08-29 19:08:43
【问题描述】:
Redis 在 python 中非常容易使用。但是,现在我在使用 Redis 事务时遇到了问题。首先,我要在Redis中得到一个key,接下来我要检查这个key绑定的值是否合法。我希望这些操作是原子的。这是我的代码。
pipe = redis_conn.pipeline()
pipe.multi()
var = pipe.get('key_want_to_be_read')
if is_legal(val):
do_something
else:
do_another_thing
pipe.execute()
但是,当我运行这些代码时,python名称var并没有绑定到存储在redis中的值,而是Pipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>,所以。有没有办法在redis事务中获取密钥并将其绑定到python名称?
【问题讨论】:
-
您应该使用
WATCH命令或Lua 脚本来完成这项工作。以this 为例。