管道
python
管道是Redis的子类,它支持缓冲多个命令,一次性发送到服务器去执行。能够大大的提升性能,减小服务器到客户端之间的TCP来回数据包。
web
管道的简单使用:redis
>>> r = redis.Redis(...) >>> r.set('bing', 'baz') >>> # Use the pipeline() method to create a pipeline instance >>> pipe = r.pipeline() >>> # The following SET commands are buffered >>> pipe.set('foo', 'bar') >>> pipe.get('bing') >>> # the EXECUTE call sends all buffered commands to the server, returning >>> # a list of responses, one for each command. >>> pipe.execute() [True, 'baz']
For ease of use, all commands being buffered into the pipeline return the pipeline object itself. Therefore calls can be chained like:服务器
为了便于使用,能够像下面这样写。
app
>>> pipe.set('foo', 'bar').sadd('faz', 'baz').incr('auto_number').execute() [True, True, 6]
In addition, pipelines can also ensure the buffered commands are executed atomically as a group. This happens by default. If you want to disable the atomic nature of a pipeline but still want to buffer commands, you can turn off transactions.ide
此外,管道还能够确保缓冲命令做为一组去执行。默认就是这样(就是管道开启了事务)。性能
要是想禁用原子性质的管道,还想缓冲命令,像下面这样设置。
ui
>>> pipe = r.pipeline(transaction=False)
A common issue occurs when requiring atomic transactions but needing to retrieve values in Redis prior for use within the transaction. For instance, let's assume that the INCR command didn't exist and we need to build an atomic version of INCR in Python.this
一个常见的问题:有些状况下须要先得到一条命令的返回值,而后在执行这个值的下一条命令,而事物中只有当全部命令都一次执行完成后才能获得每一个结果的返回值。例如,假设INCR命令不存在,咱们在python中实现带有原子性质的INCR命令。
atom
The completely naive implementation could GET the value, increment it in Python, and SET the new value back. However, this is not atomic because multiple clients could be doing this at the same time, each getting the same value from GET.
经过GET获取key存在的值,而后经过SET设置新的值,但这不是原子性质的。同一时间多个客户端能够这么操做,每一个均可以使用GET获取相同的值。
Enter the WATCH command. WATCH provides the ability to monitor one or more keys prior to starting a transaction. If any of those keys change prior the execution of that transaction, the entire transaction will be canceled and a WatchError will be raised. To implement our own client-side INCR command, we could do something like this:
WATCH命令。WATCH能够监视一个或多个key在开启事务以前。若是被监视的key在事务执行以前被修改过了,接下来的事务操做会被拒绝执行。实现咱们本身客户端的INCR命令,能够像下面这样:
>>> with r.pipeline() as pipe: ... while 1: ... try: ... pipe.watch('OUR-SEQUENCE-KEY') #监视的key ... current_value = pipe.get('OUR-SEQUENCE-KEY')#获取存在的key值 ... next_value = int(current_value) + 1#计算 ... pipe.multi()#开启事务 ... pipe.set('OUR-SEQUENCE-KEY', next_value)#设置key值 ... pipe.execute()#执行 .... break#执行成功退出循环 ... except WatchError: .... continue#若是监视的值被别的客户端动了,会引起错误,但咱们还得继续执行 #由于咱们还没让key的值改变,直到修改了key的值,才结束
Note that, because the Pipeline must bind to a single connection for the duration of a WATCH, care must be taken to ensure that the connection is returned to the connection pool by calling the reset() method. If the Pipeline is used as a context manager (as in the example above) reset() will be called automatically. Of course you can do this the manual way by explicity calling reset():
注意:由于管道在执行WATCH期间绑定到一个链接,经过调用reset()方法确保链接被放回到链接池。
若是管道只做为命令缓冲,reset方法会自动被调用,你能够手动调用复位。
>>> pipe = r.pipeline() >>> while 1: ... try: ... pipe.watch('OUR-SEQUENCE-KEY') ... ... pipe.execute() ... break ... except WatchError: ... continue ... finally: ... pipe.reset()
A convenience method named "transaction" exists for handling all the boilerplate of handling and retrying watch errors. It takes a callable that should expect a single parameter, a pipeline object, and any number of keys to be WATCHed. Our client-side INCR command above can be written like this, which is much easier to read:
存在一个名为transaction的便利方法,能够处理上面的状况,尝试下看看状况。
有一个可调用参数-管道对象,或者其余的须要WATCH监视的key.
咱们的客户端INCR命令能够写成下面这样,更简单和易读。
>>> def client_side_incr(pipe): ... current_value = pipe.get('OUR-SEQUENCE-KEY') ... next_value = int(current_value) + 1 ... pipe.multi() ... pipe.set('OUR-SEQUENCE-KEY', next_value) >>> >>> r.transaction(client_side_incr, 'OUR-SEQUENCE-KEY') [True]