2016-03-17 27 views
0

我正在使用node.js redis client。在我運行單元測試之前,我想清除所有的redis鍵。由於一些奇怪的原因,我的代碼什麼都不做。Redis客戶端不會刪除任何內容

const redis = require('redis'); 

const client = redis.createClient({ 
    host: conf.host, 
    port: conf.port, 
    prefix: conf.prefix 
}); 

client.on('connect', err => { 
    // everything is OK here. I can set and get. 
}); 

module.exports = redis; 

之前測試開始我想清空緩存:在此之後

const redis = require('file-above'); 

before(done => { 
    redis.keys('*', function (err, rows) { 
    rows.forEach(row => { 
     console.log(row); // perfectly matched 
     redis.del(row); 
     // nothing changed. I can still get any of the rows above here 
    }); 
    }); 
    done(); 
}); 

沒有被刪除。我試圖加setTimeout,試圖用直接redis.del('*', done)沒有鍵匹配。

我在redis軟件包自述文件中找不到與.del相關的任何事情,以弄清楚我做錯了什麼。

回答

1

如果適用,您應該使用flushall。在這裏查看更多文檔http://redis.io/commands/FLUSHALL。關於你的實現,我認爲你需要傳遞一個回調給del

+0

該死的del回調解決了我的問題。在網絡的某個地方發現信息.del是同步功能。好的,謝謝你的幫助 –

+0

不幸的是我錯了。沒有什麼改變。我無法使用flushall,因爲它會刪除比我想要的更多的數據。無論如何感謝您的幫助 –

+0

如果您在循環中調用''del'',您應該使用''async.js''或承諾 – gabesoft