fix: redis keys

This commit is contained in:
Namekuji 2023-09-05 15:14:19 -04:00
parent 65c62c765e
commit 1e6002cab9
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532

View file

@ -10,6 +10,7 @@ import {
UserProfiles, UserProfiles,
} from "@/models/index.js"; } from "@/models/index.js";
import { IsNull } from "typeorm"; import { IsNull } from "typeorm";
import config from "@/config/index.js";
export class Cache<T> { export class Cache<T> {
private ttl: number; private ttl: number;
@ -52,23 +53,29 @@ export class Cache<T> {
} }
public async getAll(renew = false): Promise<Map<string, T>> { public async getAll(renew = false): Promise<Map<string, T>> {
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<string, T>(); const map = new Map<string, T>();
if (keys.length === 0) { if (keys.length === 0) {
return map; return map;
} }
keys = keys.map((key) => key.slice(redisPrefix?.length));
const values = await redisClient.mgetBuffer(keys); const values = await redisClient.mgetBuffer(keys);
for (const [i, key] of keys.entries()) { for (const [i, key] of keys.entries()) {
const val = values[i]; const val = values[i];
if (val !== null) { 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) { if (renew) {
const trans = redisClient.multi(); const trans = redisClient.multi();
for (const key of map.keys()) { for (const key of keys) {
trans.expire(key, this.ttl); trans.expire(key, this.ttl);
} }
await trans.exec(); await trans.exec();