【问题标题】:Multiple Redis clients with different persistence?具有不同持久性的多个 Redis 客户端?
【发布时间】:2020-12-28 07:23:01
【问题描述】:

我需要有 3 个不同的 redis db,所以我有 3 个不同的 redis 客户端,如下所示:

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

我需要前 2 个缓存不保留,因为它们只是冷却缓存。 第三个缓存需要持久化,因为它包含一些重要的数据。

有没有办法在不同的客户端之间设置不同的持久性策略?
实现这一目标的最佳方法是什么?

我在网上搜索了一下,但没有找到答案,任何东西都非常感谢,谢谢。

对于上下文,这是我的 caches.js 文件(那里有 4 个缓存):

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

【问题讨论】:

  • 我刚刚发现所有东西都放在同一个 redisClient 中,这意味着如果我使用 setCD('key', 'value'),我可以从任何缓存中访问该值,所以即使从 getGuildSettings('key') 访问,这绝对不是我需要的。

标签: redis node-redis


【解决方案1】:

Redis 持久化配置在服务器端而不是客户端。而且一个 Redis 实例不可能有不同的持久化策略。

对于您的情况,如果您想拥有不同的持久性策略(RDB、AOF 或只是禁用),您可以设置多个 redis 服务器并为这些服务器创建不同的 redis 客户端。

对于node-redis,您可以为不同的服务器创建不同的客户端,如下所示:

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

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 2019-09-30
    • 2010-11-30
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2012-08-11
    • 2011-05-15
    相关资源
    最近更新 更多