mirror of
https://iceshrimp.dev/crimekillz/iceshrimp-161sh.git
synced 2024-11-22 12:13:48 +01:00
Fix Cache<T> .getAll and .delete functions
This commit is contained in:
parent
0ad31334e2
commit
9e5f96b9ad
@ -1,6 +1,7 @@
|
|||||||
import { redisClient } from "@/db/redis.js";
|
import { redisClient } from "@/db/redis.js";
|
||||||
import { encode, decode } from "msgpackr";
|
import { encode, decode } from "msgpackr";
|
||||||
import { ChainableCommander } from "ioredis";
|
import { ChainableCommander } from "ioredis";
|
||||||
|
import config from "@/config/index.js";
|
||||||
|
|
||||||
export class Cache<T> {
|
export class Cache<T> {
|
||||||
private ttl: number;
|
private ttl: number;
|
||||||
@ -37,12 +38,14 @@ 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 finalPrefix = `${config.cacheServer?.prefix ?? config.redis.prefix}:${this.prefix}:`;
|
||||||
|
const keys = (await redisClient.keys(`${finalPrefix}*`)).map(p => p.substring(finalPrefix.length));
|
||||||
|
const prefixedKeys = keys.map(p => this.prefixedKey(p));
|
||||||
const map = new Map<string, T>();
|
const map = new Map<string, T>();
|
||||||
if (keys.length === 0) {
|
if (keys.length === 0) {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
const values = await redisClient.mgetBuffer(keys);
|
const values = await redisClient.mgetBuffer(prefixedKeys);
|
||||||
|
|
||||||
for (const [i, key] of keys.entries()) {
|
for (const [i, key] of keys.entries()) {
|
||||||
const val = values[i];
|
const val = values[i];
|
||||||
@ -54,7 +57,7 @@ export class Cache<T> {
|
|||||||
if (renew) {
|
if (renew) {
|
||||||
const trans = redisClient.multi();
|
const trans = redisClient.multi();
|
||||||
for (const key of map.keys()) {
|
for (const key of map.keys()) {
|
||||||
trans.expire(key, this.ttl);
|
trans.expire(this.prefixedKey(key), this.ttl);
|
||||||
}
|
}
|
||||||
await trans.exec();
|
await trans.exec();
|
||||||
}
|
}
|
||||||
@ -64,7 +67,7 @@ export class Cache<T> {
|
|||||||
|
|
||||||
public async delete(...keys: (string | null)[]): Promise<void> {
|
public async delete(...keys: (string | null)[]): Promise<void> {
|
||||||
if (keys.length > 0) {
|
if (keys.length > 0) {
|
||||||
const _keys = keys.map(this.prefixedKey);
|
const _keys = keys.map(p => this.prefixedKey(p));
|
||||||
await redisClient.del(_keys);
|
await redisClient.del(_keys);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user