summaryrefslogtreecommitdiff
path: root/src/misc/cache.ts
diff options
context:
space:
mode:
authorMeiMei <30769358+mei23@users.noreply.github.com>2021-03-22 00:44:38 +0900
committerGitHub <noreply@github.com>2021-03-22 00:44:38 +0900
commitd1efe1d2085dbae14f85ab6a993e755926067446 (patch)
tree9f98ade6eb5d9c7a67d1fa429b1adf815b943835 /src/misc/cache.ts
parentfix bug (diff)
downloadsharkey-d1efe1d2085dbae14f85ab6a993e755926067446.tar.gz
sharkey-d1efe1d2085dbae14f85ab6a993e755926067446.tar.bz2
sharkey-d1efe1d2085dbae14f85ab6a993e755926067446.zip
populateEmojisのリファクタと絵文字情報のキャッシュ (#7378)
* revert * Refactor populateEmojis, Cache emojis * ん * fix typo * コメント
Diffstat (limited to 'src/misc/cache.ts')
-rw-r--r--src/misc/cache.ts23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/misc/cache.ts b/src/misc/cache.ts
index 5b7017a3b9..71fbbd8a4c 100644
--- a/src/misc/cache.ts
+++ b/src/misc/cache.ts
@@ -14,13 +14,30 @@ export class Cache<T> {
});
}
- public get(key: string | null): T | null {
+ public get(key: string | null): T | undefined {
const cached = this.cache.get(key);
- if (cached == null) return null;
+ if (cached == null) return undefined;
if ((Date.now() - cached.date) > this.lifetime) {
this.cache.delete(key);
- return null;
+ return undefined;
}
return cached.value;
}
+
+ public delete(key: string | null) {
+ this.cache.delete(key);
+ }
+
+ public async fetch(key: string | null, fetcher: () => Promise<T>): Promise<T> {
+ const cachedValue = this.get(key);
+ if (cachedValue !== undefined) {
+ // Cache HIT
+ return cachedValue;
+ }
+
+ // Cache MISS
+ const value = await fetcher();
+ this.set(key, value);
+ return value;
+ }
}