Ioredis delete all keys by pattern

I am using ioredis with express (nodejs). I know that there is a way to remove keys by template as follows:

redis-cli KEYS "sample_pattern:*" | xargs redis-cli DEL 

However, is there a way to do this using ioredis?

+5
source share
3 answers

The easiest way to delete keys from a template is to use the keys command to get the keys matching the template and then delete them one at a time, which is similar to the command line example you provided. Here is an example implemented with 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(); }); 

However, when your database has a large set of keys (say, a million), keys will lock the database for several seconds. In this case, scan more useful. ioredis has a scanStream function that helps you easily sort through a database:

 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'); }); 

Be sure to check out the official documentation for the scan command for more information: http://redis.io/commands/scan .

+14
source

I am not very versed in ioredis. But I think the * keys and the loop can handle it.

Btw, I suggest you use scan and del instead of ~

0
source

try the following commands in which you can create multiple clients for each prefix that support the get and clear set:

 // 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}; 

How to use:

 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"); 
0
source

Source: https://habr.com/ru/post/1244960/


All Articles