事物的取消使用reset()函数。
代码如下:
>>> import redis
>>> r = redis.Redis()
>>> r.set('xie', 17)
True
>>> r.set('man', 18)
True
>>> pipe = r.pipeline()
>>> pipe.multi()
>>> pipe.set('xie', 20)
>>> pipe.set('man', 20)
>>> pipe.reset()
>>> # reset之后,事务被取消,pipe.set('xie', 18)和pipe.set('man', 20)都不会执行
>>> pipe.execute()
[]
>>> r.mget('xie', 'man')
[b'17', b'18']
>>> # 'xie'和'man'还是原来的值,说明事务没有被执行
本文通过Python的redis模块演示了如何使用reset()函数来取消Redis中的事务操作。通过设置键值并尝试修改它们来展示即使在pipeline中定义了更改,通过调用reset()也可以确保这些更改不会被执行。

678

被折叠的 条评论
为什么被折叠?



