2013-10-01 59 views
1

我希望這個列表適合於提問有關redis客戶端「hiredis」的問題。 我想用redis客戶端實現同樣的功能。可以看出,redis使用一次rpush調用發送3條不同的記錄。使用hiredis通過一次呼叫發送多條記錄

redis 127.0.0.1:6379> rpush test kemal erdem husyin 

(integer) 3 
redis 127.0.0.1:6379> lrange test 0 -1 
1) "kemal" 
2) "erdem" 
3) "husyin" 

在項目中,我使用hiredis一個例子:

reply = (redisReply*)(redisCommand(c, "RPUSH %s %s" , channelName, message)); 

,但現在我有每一行正在保持像char[][]一個BUFF一個很大的日誌文件; 我需要發送每行作爲不同的記錄,但也需要調用rpush一次性能。你有什麼建議嗎?

回答

2

發送一個唯一的命令來推送超過幾千個項目將是一個壞主意。它會使通信緩衝區飽和,並且由於Redis的單線程特性,一個較大的命令會阻止所有其他併發命令。

我建議通過批量n個項目(n介於10和100之間)的小包來構建您的推送命令,並將您的推送命令組織爲m個命令的pipeline(m介於10和100之間)。

的算法將是這樣的:

While there are still lines to read: 
    New Redis pipeline, i=0 
    While there are still lines to read and i<m: 
     Read at most n lines 
     Build push command for the read lines 
     Pipeline push command 
     ++i 
    Flush Redis pipeline, check return status if needed 

它只會產生N /(n * m個)的往返(N是行中的輸入文件的數目)。

要使用任意數量的參數構建命令,可以使用redisAppendCommandArgv函數。