Services rewrite

This commit is contained in:
Emily
2024-09-18 17:43:04 +02:00
parent fa5a37ece2
commit 0be3dbecbf
40 changed files with 488146 additions and 125 deletions

View File

@@ -13,6 +13,8 @@ export const EVENT_NAMES_EXPIRE_TIME = 60;
export const EVENT_METADATA_FIELDS_EXPIRE_TIME = 30;
type UseCacheV2Callback<T> = (noStore: () => void, updateExp: (value: number) => void) => Promise<T>
export class Redis {
@@ -65,4 +67,17 @@ export class Redis {
return result;
}
static async useCacheV2<T extends any>(key: string, exp: number, callback: UseCacheV2Callback<T>) {
const cached = await this.get<T>(key);
if (cached) return cached;
let expireValue = exp;
let shouldStore = true;
const noStore = () => shouldStore = false;
const updateExp = (newExp: number) => expireValue = newExp;
const result = await callback(noStore, updateExp);
if (!shouldStore) return result;
await this.set(key, result, expireValue);
return result;
}
}