summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-03-18 10:49:14 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-03-18 10:52:54 +0900
commit8aa089178a54559cbc4e4fe84a618fc7535f178c (patch)
tree5f8cb6d5c51a37f4b667f7cd76d836f500cee1b4 /src/misc
parentImprove API performance (diff)
downloadsharkey-8aa089178a54559cbc4e4fe84a618fc7535f178c.tar.gz
sharkey-8aa089178a54559cbc4e4fe84a618fc7535f178c.tar.bz2
sharkey-8aa089178a54559cbc4e4fe84a618fc7535f178c.zip
Improve server performance
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/cache.ts25
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;
+ }
+}