2016-03-13 11 views

risposta

0

non mi piace molto di ioredis. Ma penso che le chiavi * e il ciclo possano gestirlo.

Btw, vi suggerisco che si dovrebbe utilizzare la scansione e del posto ~

13

Il modo più semplice per eliminare le chiavi di modello sta usando keys comando per ottenere le chiavi che corrispondono al modello e poi eliminarli uno ad uno, che è simile all'esempio della riga di comando che hai fornito. Ecco un esempio realizzato con 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(); 
}); 

Tuttavia quando il database ha un ampio set di chiavi (diciamo un milione), keys sarà bloccare il database per alcuni secondi. In tal caso, scan è più utile. ioredis ha scanStream funzionalità per aiutare a scorrere i dati con facilità:

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

Non dimenticare di controllare la documentazione ufficiale di scan comando per ulteriori informazioni: http://redis.io/commands/scan.

+0

Penso che usando 'unlink' è più efficiente di 'del' come ad esempio' redis.unlink (keys) 'ed elimina' pipeline' e il ciclo 'forEach'. –

0

provare i seguenti comandi in cui è possibile creare più client per ogni prefisso, che di sostegno di ottenere e chiaro:

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

Come usare:

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