我将ioredis与express (nodejs)一起使用,我知道有一种方法可以像这样按模式删除键:
redis-cli KEYS "sample_pattern:*" | xargs redis-cli DEL
然而,有没有办法使用ioredis来实现这一点呢?
发布于 2016-03-15 16:48:38
按模式删除键的最直接方法是使用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。
发布于 2022-01-08 07:34:38
首先通过pattern选择关键字,然后通过del
方法删除它们。
const keys = await ioredis.keys('PATTERN:*');
await ioredis.del(keys);
发布于 2017-11-30 10:36:30
尝试以下命令,您可以在其中为每个前缀创建多个客户端,这些客户端支持设置get和clear:
// 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");
https://stackoverflow.com/questions/35968537
复制相似问题