add clear() / dispose() methods to memory caches

This commit is contained in:
Hazelnoot 2025-10-01 11:31:35 -04:00
parent b32f3b5019
commit 8828f37b57

View file

@ -180,6 +180,11 @@ export class RedisSingleCache<T> {
await this.redisClient.del(`singlecache:${this.name}`);
}
@bindThis
public clear(): void {
this.memoryCache.delete();
}
/**
* fetcherを呼び出して結果をキャッシュ&
* This awaits the call to Redis to ensure that the write succeeded, which is important for a few reasons:
@ -208,6 +213,12 @@ export class RedisSingleCache<T> {
// TODO: イベント発行して他プロセスのメモリキャッシュも更新できるようにする
}
@bindThis
public dispose(): void {
this.clear();
}
}
}
// TODO: メモリ節約のためあまり参照されないキーを定期的に削除できるようにする?
@ -378,6 +389,11 @@ export class MemorySingleCache<T> {
this.cachedAt = null;
}
@bindThis
public clear() {
this.delete();
}
/**
* fetcherを呼び出して結果をキャッシュ&
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
@ -429,4 +445,9 @@ export class MemorySingleCache<T> {
}
return value;
}
@bindThis
public dispose() {
this.clear();
}
}