Giuliopime
Giuliopime

Reputation: 1187

Multiple Redis clients with different persistence?

I need to have 3 different redis dbs, so I have 3 different redis clients, like so:

const gCDCache = redis.createClient();
const CDCache = redis.createClient();
const vcsCache = redis.createClient();

I need the first 2 caches to not persist, seen that they are just cool-downs caches. The third cache instead needs to persist because it contains somewhat important data.

Is there a way to have different persistence policies between different client?
What is the best way to accomplish that?

I searched online a bit but couldn't find an answer, anything is appreciated, thanks.

For context, this is my caches.js file (there are 4 caches there):

 // Bot redis caches
const redis = require("redis");
const { promisify } = require("util");

// Global Cooldown cache
const gCDCache = redis.createClient();
const setGCD = promisify(gCDCache.set).bind(gCDCache);
const getGCD = promisify(gCDCache.get).bind(gCDCache);
const existsGCD = promisify(gCDCache.exists).bind(gCDCache);
const delGCD = promisify(gCDCache.del).bind(gCDCache);
gCDCache.on("error", (error) => {
    console.error(error);
});

// Cooldown cache
const CDCache = redis.createClient();
const getCD = promisify(CDCache.get).bind(CDCache);
const setCD = promisify(CDCache.set).bind(CDCache);
const existsCD = promisify(CDCache.exists).bind(CDCache);
const delCD = promisify(CDCache.del).bind(CDCache);
CDCache.on("error", (error) => {
    console.error(error);
});

// Guild Settings Cache
const gCache = redis.createClient();
const setGuildSettings = promisify(gCache.set).bind(gCache);
const getGuildSettings = promisify(gCache.get).bind(gCache);
const existsGuildSettings = promisify(gCache.exists).bind(gCache);
const delGuildSettings = promisify(gCache.del).bind(gCache);
gCache.on("error", (error) => {
    console.error(error);
});

// VCs Cache
const vcsCache = redis.createClient();
const setVCs = promisify(vcsCache.set).bind(vcsCache);
const getVCs = promisify(vcsCache.get).bind(vcsCache);

vcsCache.on("error", (error) => {
    console.error(error);
});

const caches = {
    gCache: gCache,
    setGuildSettings: setGuildSettings,
    getGuildSettings: getGuildSettings,
    existsGuildSettings: existsGuildSettings,
    delGuildSettings: delGuildSettings,

    vcsCache: vcsCache,
    setVCs: setVCs,
    getVCs: getVCs,

    gCDCache: gCDCache,
    setGCD: setGCD,
    getGCD: getGCD,
    existsGCD: existsGCD,
    delGCD: delGCD,

    CDCache: CDCache,
    getCD: getCD,
    setCD: setCD,
    existsCD: existsCD,
    delCD: delCD
}

module.exports.caches = caches;

console.log('Astro\'s Caches Loaded');

Upvotes: 1

Views: 369

Answers (1)

Gawain
Gawain

Reputation: 1092

Redis persistences are configured in server side not client side. And it is not possible to have different persistence policy for one Redis instance.

For you case, if you want to have different persistence policy(RDB, AOF, or just disabled), you can set up multiple redis servers and create different redis client for those servers.

For node-redis, you can create different client for different servers as below:

redis.createClient([options])
redis.createClient(unix_socket[, options])
redis.createClient(redis_url[, options])
redis.createClient(port[, host][, options])

Upvotes: 1

Related Questions