From 7228c47ddd9c89e2614a1af23e9a5529be2e3537 Mon Sep 17 00:00:00 2001 From: Kei Kamikawa Date: Sat, 24 Feb 2024 12:50:02 +0900 Subject: [PATCH] fixed singleton behaviour for WorkersKVStoreSingle --- src/index.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 507b8b3..70ae6c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,7 +35,7 @@ export class Auth extends BaseAuth { } export class WorkersKVStoreSingle extends WorkersKVStore { - private static instance?: WorkersKVStoreSingle; + private static instance?: Map; private constructor(cacheKey: string, cfKVNamespace: KVNamespace) { super(cacheKey, cfKVNamespace); @@ -43,9 +43,15 @@ export class WorkersKVStoreSingle extends WorkersKVStore { static getOrInitialize(cacheKey: string, cfKVNamespace: KVNamespace): WorkersKVStoreSingle { if (!WorkersKVStoreSingle.instance) { - WorkersKVStoreSingle.instance = new WorkersKVStoreSingle(cacheKey, cfKVNamespace); + WorkersKVStoreSingle.instance = new Map(); } - return WorkersKVStoreSingle.instance; + const instance = WorkersKVStoreSingle.instance.get(cacheKey); + if (instance) { + return instance; + } + const newInstance = new WorkersKVStoreSingle(cacheKey, cfKVNamespace); + WorkersKVStoreSingle.instance.set(cacheKey, newInstance); + return newInstance; } }