summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/cache.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-03-21 01:22:00 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2022-03-21 01:22:00 +0900
commiteb9e6d230f41798615e45f00b036e0a48e3ac044 (patch)
tree1c0e368f02eaa1ca9597d2063ac69dfd8906b733 /packages/backend/src/misc/cache.ts
parentperf(server): reduce db query (diff)
downloadsharkey-eb9e6d230f41798615e45f00b036e0a48e3ac044.tar.gz
sharkey-eb9e6d230f41798615e45f00b036e0a48e3ac044.tar.bz2
sharkey-eb9e6d230f41798615e45f00b036e0a48e3ac044.zip
perf(server): reduce db query
Diffstat (limited to 'packages/backend/src/misc/cache.ts')
-rw-r--r--packages/backend/src/misc/cache.ts17
1 files changed, 14 insertions, 3 deletions
diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts
index 76835b44b1..e9966b7785 100644
--- a/packages/backend/src/misc/cache.ts
+++ b/packages/backend/src/misc/cache.ts
@@ -28,11 +28,22 @@ export class Cache<T> {
this.cache.delete(key);
}
- public async fetch(key: string | null, fetcher: () => Promise<T>): Promise<T> {
+ /**
+ * キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
+ * optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
+ */
+ public async fetch(key: string | null, fetcher: () => Promise<T>, validator?: (cachedValue: T) => boolean): Promise<T> {
const cachedValue = this.get(key);
if (cachedValue !== undefined) {
- // Cache HIT
- return cachedValue;
+ if (validator) {
+ if (validator(cachedValue)) {
+ // Cache HIT
+ return cachedValue;
+ }
+ } else {
+ // Cache HIT
+ return cachedValue;
+ }
}
// Cache MISS