From d1efe1d2085dbae14f85ab6a993e755926067446 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Mon, 22 Mar 2021 00:44:38 +0900 Subject: populateEmojisのリファクタと絵文字情報のキャッシュ (#7378) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * revert * Refactor populateEmojis, Cache emojis * ん * fix typo * コメント --- src/misc/cache.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/misc/cache.ts') 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 { }); } - 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): Promise { + const cachedValue = this.get(key); + if (cachedValue !== undefined) { + // Cache HIT + return cachedValue; + } + + // Cache MISS + const value = await fetcher(); + this.set(key, value); + return value; + } } -- cgit v1.2.3-freya