@@ -2,11 +2,13 @@ import { Cache, CacheConfiguration, Loader } from '../DataSources'
2
2
import type { Redis } from 'ioredis'
3
3
4
4
export interface RedisCacheConfiguration extends CacheConfiguration {
5
+ prefix : string
5
6
json : boolean
6
7
}
7
8
8
9
const DefaultConfiguration : RedisCacheConfiguration = {
9
10
json : false ,
11
+ prefix : 'layered-cache:' ,
10
12
ttlInMsecs : 1000 * 60 * 10 ,
11
13
}
12
14
@@ -26,11 +28,11 @@ export class RedisCache<T> implements Cache<T>, Loader<T> {
26
28
}
27
29
28
30
async delete ( key : string ) : Promise < void > {
29
- await this . redis . del ( key )
31
+ await this . redis . del ( this . resolveKey ( key ) )
30
32
}
31
33
32
34
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 ) )
34
36
if ( redisResult && this . config . json ) {
35
37
return JSON . parse ( redisResult )
36
38
}
@@ -48,9 +50,13 @@ export class RedisCache<T> implements Cache<T>, Loader<T> {
48
50
const resolvedValue : string = value && this . config . json ? JSON . stringify ( value ) : ( value as unknown as string )
49
51
50
52
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 )
52
54
return
53
55
}
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 } `
55
61
}
56
62
}
0 commit comments