diff --git a/packages/core/lib/cache/drivers/dice-db.ts b/packages/core/lib/cache/drivers/dice-db.ts index 58fceaa..f1ac883 100644 --- a/packages/core/lib/cache/drivers/dice-db.ts +++ b/packages/core/lib/cache/drivers/dice-db.ts @@ -11,7 +11,7 @@ export class DiceDbDriver implements CacheDriver { async get(key: string): Promise { await this.initializeModules(); - const value = await this.client.get(`${this.options.prefix}:::${key}`); + const value = await this.client.get(this.storeKey(key)); if (!value) return null; try { return JSON.parse(value); @@ -27,7 +27,7 @@ export class DiceDbDriver implements CacheDriver { ): Promise { await this.initializeModules(); try { - const redisKey = `${this.options.prefix}:::${key}`; + const redisKey = this.storeKey(key); ttlInSec ? await this.client.set(redisKey, JSON.stringify(value), 'EX', ttlInSec) : await this.client.set(redisKey, JSON.stringify(value)); @@ -39,7 +39,7 @@ export class DiceDbDriver implements CacheDriver { async has(key: string): Promise { await this.initializeModules(); - const num = await this.client.exists(`${this.options.prefix}:::${key}`); + const num = await this.client.exists(this.storeKey(key)); return !!num; } diff --git a/packages/core/lib/cache/drivers/inMemory.ts b/packages/core/lib/cache/drivers/inMemory.ts index 66b3754..e479fb5 100644 --- a/packages/core/lib/cache/drivers/inMemory.ts +++ b/packages/core/lib/cache/drivers/inMemory.ts @@ -11,7 +11,7 @@ export class InMemoryDriver implements CacheDriver { async get(key: string): Promise { await this.initialiseModules(); - const value = await this.client.get(`${this.options.prefix}:::${key}`); + const value = await this.client.get(this.storeKey(key)); if (!value) return null; try { return JSON.parse(value); @@ -26,7 +26,7 @@ export class InMemoryDriver implements CacheDriver { ttlInSec?: number | undefined, ): Promise { await this.initialiseModules(); - const cacheKey = `${this.options.prefix}:::${key}`; + const cacheKey = this.storeKey(key); if (ttlInSec) { return this.client.set(cacheKey, value, ttlInSec); @@ -37,7 +37,7 @@ export class InMemoryDriver implements CacheDriver { async has(key: string): Promise { await this.initialiseModules(); - const cacheKey = `${this.options.prefix}:::${key}`; + const cacheKey = this.storeKey(key); return this.client.has(cacheKey); } @@ -75,7 +75,7 @@ export class InMemoryDriver implements CacheDriver { async forget(key: string): Promise { await this.initialiseModules(); try { - const cacheKey = `${this.options.prefix}:::${key}`; + const cacheKey = this.storeKey(key); await this.client.del(cacheKey); return true; } catch { @@ -83,6 +83,10 @@ export class InMemoryDriver implements CacheDriver { } } + private storeKey(key: string): string { + return `${this.options.prefix}:::${key}`; + } + getClient(): T { return this.client as unknown as T; } diff --git a/packages/core/lib/cache/drivers/redis.ts b/packages/core/lib/cache/drivers/redis.ts index 4201c1e..4f4a988 100644 --- a/packages/core/lib/cache/drivers/redis.ts +++ b/packages/core/lib/cache/drivers/redis.ts @@ -12,7 +12,7 @@ export class RedisDriver implements CacheDriver { async get(key: string): Promise { await this.initializeModules(); - const value = await this.client.get(`${this.options.prefix}:::${key}`); + const value = await this.client.get(this.storeKey(key)); if (!value) return null; try { return JSON.parse(value); @@ -28,7 +28,7 @@ export class RedisDriver implements CacheDriver { ): Promise { await this.initializeModules(); try { - const redisKey = `${this.options.prefix}:::${key}`; + const redisKey = this.storeKey(key); ttlInSec ? await this.client.set(redisKey, JSON.stringify(value), 'EX', ttlInSec) : await this.client.set(redisKey, JSON.stringify(value)); @@ -40,7 +40,7 @@ export class RedisDriver implements CacheDriver { async has(key: string): Promise { await this.initializeModules(); - const num = await this.client.exists(`${this.options.prefix}:::${key}`); + const num = await this.client.exists(this.storeKey(key)); return !!num; }