【发布时间】: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