Skip to content

Commit 4f8ba8a

Browse files
committed
Add prefix
1 parent 4249108 commit 4f8ba8a

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

lib/redis/RedisCache.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { Cache, CacheConfiguration, Loader } from '../DataSources'
22
import type { Redis } from 'ioredis'
33

44
export interface RedisCacheConfiguration extends CacheConfiguration {
5+
prefix: string
56
json: boolean
67
}
78

89
const DefaultConfiguration: RedisCacheConfiguration = {
910
json: false,
11+
prefix: 'layered-cache:',
1012
ttlInMsecs: 1000 * 60 * 10,
1113
}
1214

@@ -26,11 +28,11 @@ export class RedisCache<T> implements Cache<T>, Loader<T> {
2628
}
2729

2830
async delete(key: string): Promise<void> {
29-
await this.redis.del(key)
31+
await this.redis.del(this.resolveKey(key))
3032
}
3133

3234
async get(key: string): Promise<T | undefined> {
33-
const redisResult = await this.redis.get(key)
35+
const redisResult = await this.redis.get(this.resolveKey(key))
3436
if (redisResult && this.config.json) {
3537
return JSON.parse(redisResult)
3638
}
@@ -48,9 +50,13 @@ export class RedisCache<T> implements Cache<T>, Loader<T> {
4850
const resolvedValue: string = value && this.config.json ? JSON.stringify(value) : (value as unknown as string)
4951

5052
if (this.config.ttlInMsecs) {
51-
await this.redis.set(key, resolvedValue, 'PX', this.config.ttlInMsecs)
53+
await this.redis.set(this.resolveKey(key), resolvedValue, 'PX', this.config.ttlInMsecs)
5254
return
5355
}
54-
await this.redis.set(key, resolvedValue)
56+
await this.redis.set(this.resolveKey(key), resolvedValue)
57+
}
58+
59+
resolveKey(key: string) {
60+
return `${this.config.prefix}${key}`
5561
}
5662
}

test/RedisCache.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import Redis from 'ioredis'
1+
import Redis, { RedisOptions } from 'ioredis'
22
import { RedisCache } from '../lib/redis/RedisCache'
33

4-
const redisOptions: Redis.RedisOptions = {
4+
const redisOptions: RedisOptions = {
55
host: 'localhost',
66
port: 6379,
77
password: 'sOmE_sEcUrE_pAsS',
88
}
99

1010
describe('RedisCache', () => {
11-
let redis: Redis.Redis
11+
let redis: Redis
1212
beforeEach(async () => {
1313
redis = new Redis(redisOptions)
1414
})
@@ -52,6 +52,8 @@ describe('RedisCache', () => {
5252
it('sets json values correctly', async () => {
5353
const cache = new RedisCache(redis, {
5454
json: true,
55+
prefix: 'cache',
56+
ttlInMsecs: 5000,
5557
})
5658
await cache.set('key', { value: 'value' })
5759
await cache.set('key2', { value: 'value2' })
@@ -67,6 +69,7 @@ describe('RedisCache', () => {
6769
const cache = new RedisCache(redis, {
6870
json: true,
6971
ttlInMsecs: 10000,
72+
prefix: 'cache:',
7073
})
7174
await cache.set('key', { value: 'value' })
7275
await cache.set('key2', { value: 'value2' })

0 commit comments

Comments
 (0)