Skip to content

Commit d397d4e

Browse files
authored
Merge pull request #79 from yatin166/refactor-cache-drivers
refactor(core): added storekey for consistency in cache driver
2 parents 9b5181e + a4623e1 commit d397d4e

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

packages/core/lib/cache/drivers/dice-db.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class DiceDbDriver implements CacheDriver {
1111

1212
async get(key: string): Promise<any> {
1313
await this.initializeModules();
14-
const value = await this.client.get(`${this.options.prefix}:::${key}`);
14+
const value = await this.client.get(this.storeKey(key));
1515
if (!value) return null;
1616
try {
1717
return JSON.parse(value);
@@ -27,7 +27,7 @@ export class DiceDbDriver implements CacheDriver {
2727
): Promise<boolean> {
2828
await this.initializeModules();
2929
try {
30-
const redisKey = `${this.options.prefix}:::${key}`;
30+
const redisKey = this.storeKey(key);
3131
ttlInSec
3232
? await this.client.set(redisKey, JSON.stringify(value), 'EX', ttlInSec)
3333
: await this.client.set(redisKey, JSON.stringify(value));
@@ -39,7 +39,7 @@ export class DiceDbDriver implements CacheDriver {
3939

4040
async has(key: string): Promise<boolean> {
4141
await this.initializeModules();
42-
const num = await this.client.exists(`${this.options.prefix}:::${key}`);
42+
const num = await this.client.exists(this.storeKey(key));
4343
return !!num;
4444
}
4545

packages/core/lib/cache/drivers/inMemory.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class InMemoryDriver implements CacheDriver {
1111

1212
async get<T>(key: string): Promise<T> {
1313
await this.initialiseModules();
14-
const value = await this.client.get(`${this.options.prefix}:::${key}`);
14+
const value = await this.client.get(this.storeKey(key));
1515
if (!value) return null;
1616
try {
1717
return JSON.parse(value);
@@ -26,7 +26,7 @@ export class InMemoryDriver implements CacheDriver {
2626
ttlInSec?: number | undefined,
2727
): Promise<boolean> {
2828
await this.initialiseModules();
29-
const cacheKey = `${this.options.prefix}:::${key}`;
29+
const cacheKey = this.storeKey(key);
3030

3131
if (ttlInSec) {
3232
return this.client.set(cacheKey, value, ttlInSec);
@@ -37,7 +37,7 @@ export class InMemoryDriver implements CacheDriver {
3737

3838
async has(key: string): Promise<boolean> {
3939
await this.initialiseModules();
40-
const cacheKey = `${this.options.prefix}:::${key}`;
40+
const cacheKey = this.storeKey(key);
4141
return this.client.has(cacheKey);
4242
}
4343

@@ -75,14 +75,18 @@ export class InMemoryDriver implements CacheDriver {
7575
async forget(key: string): Promise<boolean> {
7676
await this.initialiseModules();
7777
try {
78-
const cacheKey = `${this.options.prefix}:::${key}`;
78+
const cacheKey = this.storeKey(key);
7979
await this.client.del(cacheKey);
8080
return true;
8181
} catch {
8282
return false;
8383
}
8484
}
8585

86+
private storeKey(key: string): string {
87+
return `${this.options.prefix}:::${key}`;
88+
}
89+
8690
getClient<T>(): T {
8791
return this.client as unknown as T;
8892
}

packages/core/lib/cache/drivers/redis.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class RedisDriver implements CacheDriver {
1212

1313
async get(key: string): Promise<any> {
1414
await this.initializeModules();
15-
const value = await this.client.get(`${this.options.prefix}:::${key}`);
15+
const value = await this.client.get(this.storeKey(key));
1616
if (!value) return null;
1717
try {
1818
return JSON.parse(value);
@@ -28,7 +28,7 @@ export class RedisDriver implements CacheDriver {
2828
): Promise<boolean> {
2929
await this.initializeModules();
3030
try {
31-
const redisKey = `${this.options.prefix}:::${key}`;
31+
const redisKey = this.storeKey(key);
3232
ttlInSec
3333
? await this.client.set(redisKey, JSON.stringify(value), 'EX', ttlInSec)
3434
: await this.client.set(redisKey, JSON.stringify(value));
@@ -40,7 +40,7 @@ export class RedisDriver implements CacheDriver {
4040

4141
async has(key: string): Promise<boolean> {
4242
await this.initializeModules();
43-
const num = await this.client.exists(`${this.options.prefix}:::${key}`);
43+
const num = await this.client.exists(this.storeKey(key));
4444
return !!num;
4545
}
4646

0 commit comments

Comments
 (0)