From 1e6002cab9b148d4d3f1a36696c33089643e0279 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Tue, 5 Sep 2023 15:14:19 -0400 Subject: [PATCH] fix: redis keys --- packages/backend/src/misc/cache.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts index 5c5bbacaac..349c83d410 100644 --- a/packages/backend/src/misc/cache.ts +++ b/packages/backend/src/misc/cache.ts @@ -10,6 +10,7 @@ import { UserProfiles, } from "@/models/index.js"; import { IsNull } from "typeorm"; +import config from "@/config/index.js"; export class Cache { private ttl: number; @@ -52,23 +53,29 @@ export class Cache { } public async getAll(renew = false): Promise> { - const keys = await redisClient.keys(`${this.prefix}*`); + const redisPrefix = `${config.cacheServer?.prefix ?? config.redis.prefix}:`; + let keys = await redisClient.keys(`${redisPrefix}${this.prefix}*`); const map = new Map(); if (keys.length === 0) { return map; } + keys = keys.map((key) => key.slice(redisPrefix?.length)); const values = await redisClient.mgetBuffer(keys); for (const [i, key] of keys.entries()) { const val = values[i]; if (val !== null) { - map.set(key, decode(val) as T); + let mapKey = key.substring(this.prefix.length); + if (mapKey.indexOf(":") === 0) { + mapKey = mapKey.substring(1); + } + map.set(mapKey, decode(val) as T); } } if (renew) { const trans = redisClient.multi(); - for (const key of map.keys()) { + for (const key of keys) { trans.expire(key, this.ttl); } await trans.exec();