populateEmojisのリファクタと絵文字情報のキャッシュ (#7378)

* revert

* Refactor populateEmojis, Cache emojis

* ん

* fix typo

* コメント
This commit is contained in:
MeiMei
2021-03-22 00:44:38 +09:00
committed by GitHub
parent 2f2a8e537d
commit d1efe1d208
5 changed files with 89 additions and 216 deletions

View File

@@ -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;
}
}