首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ioredis按模式删除所有键

ioredis按模式删除所有键
EN

Stack Overflow用户
提问于 2016-03-13 17:17:01
回答 4查看 15.8K关注 0票数 12

我将ioredis与express (nodejs)一起使用,我知道有一种方法可以像这样按模式删除键:

代码语言:javascript
运行
复制
redis-cli KEYS "sample_pattern:*" | xargs redis-cli DEL

然而,有没有办法使用ioredis来实现这一点呢?

EN

回答 4

Stack Overflow用户

发布于 2016-03-15 16:48:38

按模式删除键的最直接方法是使用keys命令获取与模式匹配的键,然后逐个删除它们,这类似于您提供的命令行示例。下面是一个使用ioredis实现的示例:

代码语言:javascript
运行
复制
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功能,可以帮助您轻松遍历数据库:

代码语言:javascript
运行
复制
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

票数 32
EN

Stack Overflow用户

发布于 2022-01-08 07:34:38

首先通过pattern选择关键字,然后通过del方法删除它们。

代码语言:javascript
运行
复制
const keys = await ioredis.keys('PATTERN:*');

await ioredis.del(keys);
票数 2
EN

Stack Overflow用户

发布于 2017-11-30 10:36:30

尝试以下命令,您可以在其中为每个前缀创建多个客户端,这些客户端支持设置get和clear:

代码语言:javascript
运行
复制
// 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};

使用方法:

代码语言:javascript
运行
复制
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");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35968537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档