python - Python 中的 Redis : difference of with and without multi() function

标签 python redis

redis 中,它说:

A Redis transaction is entered using the MULTI command.

对于它的 Python API:

r = redis.Redis(...)
pipe = r.pipeline()
current_value = pipe.get('someKey')
#pipe.multi()
pipe.set('someKey', current_value + 1)
pipe.execute()

使用和不使用 pipe.multi() 有什么区别?

要保证其原子性,正确的解决方案是什么?

最佳答案

流水线 是一种可以节省(RTT) 往返时间的机制,当您实际上想要批处理 key 的更新/查询并且您不需要每个键本身的回复。

Multi 实际上会使事务原子化;所以一组命令被组合成一个命令,example会是:

MULTI 
SADD foo a 
SADD foo b 
EXEC 

如果有人在事务发生之前或期间查询 foo 的值,他们将得到 NULL 而有人在事务完成后查询将得到 a ,b ,因此您不可能单独获得 a

Python/Redis API 引用默认将其设置为 MULTI,如果不需要,您可以将其设置为 false,MULTI/EXEC:这些是作为流水线类。管道在执行时默认使用 MULTI 和 EXEC 语句包装,可以通过指定 transaction=False 来禁用。 Reference

关于python - Python 中的 Redis : difference of with and without multi() function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33695837/

相关文章:

database - 如何使用redis高效实现嵌套评论系统

ruby-on-rails - ruby 和 Redis : set a timeout for subscribtions

python - 什么是单一引用的高效容器?

python - 导入错误: No module named 'xgboost'

python - Tkinter。使用 "different"命令函数创建多个按钮

java - 将图像从套接字服务器(java)发送到套接字客户端(python)

logging - Logstash 输入插件 : Redis vs Elasticsearch

caching - Redis 缓存关系数据的正确策略

redis - 如何设置redis连接超时?

python - 将 jsonschema 转换为 Django REST 序列化器的工具?