2015-10-22 45 views
1

我想了解,redis中的管道襯裏是如何工作的?根據一個博客我看了,對於這個代碼redis管道在pyredis中如何工作?

Pipeline pipeline = jedis.pipelined(); 
long start = System.currentTimeMillis(); 
for (int i = 0; i < 100000; i++) { 
    pipeline.set("" + i, "" + i); 
} 
List<Object> results = pipeline.execute(); 

Every call to pipeline.set() effectively sends the SET command to Redis (you can easily see this by setting a breakpoint inside the loop and querying Redis with redis-cli). The call to pipeline.execute() is when the reading of all the pending responses happens.

因此,基本上,當我們用管內襯,當我們執行任意命令,像上面設定,命令被在服務器上執行,但我們在我們執行pipeline.execute()之前不要收集響應。

然而,根據pyredis的文檔, Pipelines are a subclass of the base Redis class that provide support for buffering multiple commands to the server in a single request.

我認爲,這意味着,我們使用流水線,所有的命令進行緩衝,併發送到服務器,當我們執行pipe.execute() ,所以這種行爲與上述行爲不同。

有人能告訴我使用pyreids時什麼是正確的行爲?

回答

0

我確實運行了您從博客中描述的測試,但無法重現該行爲。 中設置斷點for循環,並運行

redis-cli info | grep keys 

不顯示大小每一套命令後增加。說到這個,你粘貼的代碼似乎是使用Jedis的Java(我也使用過)。 而在我跑的測試,並根據該文檔,沒有方法執行()在jedis而是EXEC()同步()之一。

我在sync()命令後看到了在redis中設置的值。

此外,this question似乎與pyredis文檔。

最後,redis documentation本身專注於網絡優化(引用的例子)

這一次,我們不支付RTT的每一個電話的成本,但只是一個用於三個命令的時間。

P.S.你能看到你閱讀的博客鏈接嗎?

1

這不僅僅是一個redis-py的東西。在Redis中,流水線總是表示緩衝一組命令,然後將它們一次發送到服務器。流水線的要點是避免無關的網絡來回 - 往往是對Redis運行命令時的瓶頸。如果每個命令在管道運行之前都發送給Redis,情況就不會如此。

你可以在實踐中測試這個。打開python和:

import redis 
r = redis.Redis() 
p = r.pipeline() 
p.set('blah', 'foo') # this buffers the command. it is not yet run. 
r.get('blah') # pipeline hasn't been run, so this returns nothing. 
p.execute() 
r.get('blah') # now that we've run the pipeline, this returns "foo".