2012-10-06 62 views
1

我有一個問題,EXEC的回調隨機返回一些值,如null s。Redis MULTI事務在NodeJS的EXEC回調中隨機返回null

的代碼工作正常,大部分的時間,但它隨機失敗(或者我刷新多次瀏覽器)...

這裏是淪落到這種地步的代碼,其中它的失敗:

var transaction = client.multi(); 
reply.forEach(function (id) { // reply always equals [ 'mykey1', 'mykey2' ] 
    transaction.hgetall(namespace + ":" + id); 
}); 
transaction.exec(function (err, replies) { 
    // 'replies' sometimes returns all the responses properly, 
    // other times it returns some of the values as null 
    // See the examples I wrote below 
}); 

當它工作正常,在EXEC回調返回此:

[{ 
    owner: '123', 
    id: 'asdasdasd', 
    name: 'asdasdasd', 
    created_at: '2012-10-06T09:26:25.596Z', 
    updated_at: '2012-10-06T09:28:54.929Z' 
}, 
{ 
    owner: '456', 
    id: 'asdfsdfasdf', 
    name: 'asdfsdfasdf', 
    created_at: '2012-10-06T09:27:19.251Z', 
    updated_at: '2012-10-06T09:28:03.116Z' 
}] 

如果它不能正常工作,它返回此:(注意null值)

[{ 
    owner: '123', 
    id: 'asdasdasd', 
    name: 'asdasdasd', 
    created_at: '2012-10-06T09:26:25.596Z', 
    updated_at: '2012-10-06T09:28:54.929Z' 
}, null] 
+1

聽起來像你有某種地方的競爭條件。你是在其他地方刪除密鑰還是設置過期? – Bill

+0

關於失敗,在調用exec之前'console.log(transaction)'的輸出是什麼樣的?它應該顯示你的電話。 – sintaxi

回答

0

什麼是您的redis版本?

2.6.5之前的版本即使某些命令未能排隊也會執行事務 - 您需要檢查事務塊中的每個命令是否失敗(使用回調的錯誤參數)。

如果您使用的是Redis 2.6.5或更高版本,那麼EXEC命令與沒有正確排隊的命令會返回錯誤,所以這不應該是您的情況,並且可能的解釋是一些競爭條件或不存在的關鍵暗示在上面表揚。 hgetall的「null」回覆意味着密鑰不存在 - 所以在執行事務時它可能不存在?