我使用ioredis與快遞(的NodeJS) 全部刪除鍵我知道有通過模式這樣的刪除鍵的方式:ioredis通過模式
redis-cli KEYS "sample_pattern:*" | xargs redis-cli DEL
但是,有沒有辦法做到使用ioredis代替?
我使用ioredis與快遞(的NodeJS) 全部刪除鍵我知道有通過模式這樣的刪除鍵的方式:ioredis通過模式
redis-cli KEYS "sample_pattern:*" | xargs redis-cli DEL
但是,有沒有辦法做到使用ioredis代替?
我對ioredis沒有太多瞭解。 但我認爲鍵*和for循環可以處理它。
順便說一句,我建議你應該使用掃描和刪除,而不是〜
最直接的方法來刪除通過模式按鍵使用keys
命令來獲取匹配模式的鍵,然後刪除它們一個接一個,這與您提供的命令行示例類似。下面是與ioredis實現的一個例子:
var Redis = require('ioredis');
var redis = new Redis();
redis.keys('sample_pattern:*').then(function (keys) {
// Use pipeline instead of sending
// one command each time to improve the
// performance.
var pipeline = redis.pipeline();
keys.forEach(function (key) {
pipeline.del(key);
});
return pipeline.exec();
});
然而,當你的數據庫有一個大組鍵(比如說一百萬),keys
將阻止數據庫幾秒鐘。在這種情況下,scan
更有用。 ioredis有scanStream
功能幫助您遍歷輕鬆的數據庫:
var Redis = require('ioredis');
var redis = new Redis();
// Create a readable stream (object mode)
var stream = redis.scanStream({
match: 'sample_pattern:*'
});
stream.on('data', function (keys) {
// `keys` is an array of strings representing key names
if (keys.length) {
var pipeline = redis.pipeline();
keys.forEach(function (key) {
pipeline.del(key);
});
pipeline.exec();
}
});
stream.on('end', function() {
console.log('done');
});
不要忘了有關詳細信息,請查看scan
命令的官方文檔:http://redis.io/commands/scan。
試試下面的命令,你可以爲每個前綴創建多個客戶端,支持設置得和明確:
// myredis.js
const Redis = require('ioredis');
const ConnectRedis = require('connect-redis');
const config = {}; // your ioredis config
const clients = {};
/**
* @private create redis client
* @param {string} name client name
* @param {boolean} isSession is this the application session client or not
* @return {Redis|*}
*/
const createClient = (name, isSession = false) => {
let client;
client = new Redis({...config, "keyPrefix":`${name}:`)});
client.on('error', msg => console.log("Redis Client[" + name + "]: " + msg));
client.on('connect',() => console.log("Redis Client[" + name + "]: Connected"));
if (isSession) {
const RedisStore = ConnectRedis(isSession);
client = new RedisStore({client});
}
return client;
};
/**
* Create or get redis client
* @param {string} name client name
* @return {Redis|*}
*/
const getClient = name => {
let client = clients[name];
if (!client || !client.connected) {
client = clients[name] = createClient(name);
}
return client;
};
/**
* get keys only related to this client prefix
* @param name
*/
const getClientKeys = name => getClient(name).keys(`${name}:*`).then(keys => keys.map(key => key.substr(name.length + 1)));
/**
* clear client
* @param name
*/
const clearClient = name => getClientKeys(name).then(keys => {
const client = getClient(name);
client && keys.forEach(key => client.del(key))
});
module.exports = {getClient, clearClient, getClientKeys};
使用方法:
const {getClient, clearClient} = require("./myredis");
// this will get a client with prefix "marvel:" and if it is not exists it will be created
const client = getClient("marvel");
// set value
client.set("fav", "ironman");
// get the value
client.get("fav", (error, value) => console.log(value));
// clear client
clearClient("marvel");
我認爲使用'unlink'比'del'更有效,比如'redis.unlink(keys)',並刪除'pipeline'和'forEach'循環。 –