Support dragonfly with in memory cache

This commit is contained in:
Ajay
2024-02-09 18:16:28 -05:00
parent 9fa248037a
commit 6d11e1c601
3 changed files with 16 additions and 5 deletions

View File

@@ -167,7 +167,8 @@ addDefaults(config, {
responseTimePause: 1000, responseTimePause: 1000,
disableHashCache: false, disableHashCache: false,
clientCacheSize: 2000, clientCacheSize: 2000,
useCompression: false useCompression: false,
dragonflyMode: false
}, },
redisRead: { redisRead: {
enabled: false, enabled: false,

View File

@@ -13,6 +13,7 @@ interface RedisConfig extends redis.RedisClientOptions {
disableHashCache: boolean; disableHashCache: boolean;
clientCacheSize: number; clientCacheSize: number;
useCompression: boolean; useCompression: boolean;
dragonflyMode: boolean;
} }
interface RedisReadOnlyConfig extends redis.RedisClientOptions { interface RedisReadOnlyConfig extends redis.RedisClientOptions {

View File

@@ -180,6 +180,12 @@ if (config.redis?.enabled) {
const del = client.del.bind(client); const del = client.del.bind(client);
exportClient.del = (...keys) => { exportClient.del = (...keys) => {
if (config.redis.dragonflyMode) {
for (const key of keys) {
void client.publish("__redis__:invalidate", key);
}
}
if (config.redis.useCompression) { if (config.redis.useCompression) {
return del(...keys.map((key) => createKeyName(key)) as [RedisCommandArgument]); return del(...keys.map((key) => createKeyName(key)) as [RedisCommandArgument]);
} else { } else {
@@ -367,12 +373,15 @@ export function getRedisStats(): RedisStats {
async function setupCacheClientListener(cacheClient: RedisClientType, async function setupCacheClientListener(cacheClient: RedisClientType,
cache: LRUCache<RedisCommandArgument, string>) { cache: LRUCache<RedisCommandArgument, string>) {
if (!config.redis.dragonflyMode) {
cacheConnectionClientId = String(await cacheClient.clientId()); cacheConnectionClientId = String(await cacheClient.clientId());
}
cacheClient.subscribe("__redis__:invalidate", (keys) => { cacheClient.subscribe("__redis__:invalidate", (message) => {
if (keys) { if (message) {
lastInvalidationMessage = Date.now(); lastInvalidationMessage = Date.now();
const keys = Buffer.isBuffer(message) ? [message.toString()] : message;
for (let key of keys) { for (let key of keys) {
if (config.redis.useCompression) key = key.replace(/.c$/, ""); if (config.redis.useCompression) key = key.replace(/.c$/, "");
@@ -392,7 +401,7 @@ async function setupCacheClientListener(cacheClient: RedisClientType,
async function setupCacheClientTracking(client: RedisClientType, async function setupCacheClientTracking(client: RedisClientType,
cacheClient: RedisClientType) { cacheClient: RedisClientType) {
if (!client || !cacheClient.isReady) return; if (!client || !cacheClient.isReady || config.redis.dragonflyMode) return;
await client.sendCommand(["CLIENT", "TRACKING", "ON", "REDIRECT", cacheConnectionClientId, "BCAST"]); await client.sendCommand(["CLIENT", "TRACKING", "ON", "REDIRECT", cacheConnectionClientId, "BCAST"]);
} }