diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2021-03-18 10:49:14 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2021-03-18 10:52:54 +0900 |
| commit | 8aa089178a54559cbc4e4fe84a618fc7535f178c (patch) | |
| tree | 5f8cb6d5c51a37f4b667f7cd76d836f500cee1b4 /src/misc | |
| parent | Improve API performance (diff) | |
| download | sharkey-8aa089178a54559cbc4e4fe84a618fc7535f178c.tar.gz sharkey-8aa089178a54559cbc4e4fe84a618fc7535f178c.tar.bz2 sharkey-8aa089178a54559cbc4e4fe84a618fc7535f178c.zip | |
Improve server performance
Diffstat (limited to 'src/misc')
| -rw-r--r-- | src/misc/cache.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/misc/cache.ts b/src/misc/cache.ts new file mode 100644 index 0000000000..356a3de7b9 --- /dev/null +++ b/src/misc/cache.ts @@ -0,0 +1,25 @@ +export class Cache<T> { + private cache: Map<string | null, { date: number; value: T; }>; + private lifetime: number; + + constructor(lifetime: Cache<never>['lifetime']) { + this.lifetime = lifetime; + } + + public set(key: string | null, value: T):void { + this.cache.set(key, { + date: Date.now(), + value + }); + } + + public get(key: string | null): T | null { + const cached = this.cache.get(key); + if (cached == null) return null; + if ((Date.now() - cached.date) > this.lifetime) { + this.cache.delete(key); + return null; + } + return cached.value; + } +} |