@@ -3,6 +3,7 @@ import { logger } from 'srcCommon/logger';
3
3
import { PHASE_PRODUCTION_BUILD } from 'next/constants' ;
4
4
import { CACHE_TTL_24_HOURS_IN_MS } from 'srcCommon/constants' ;
5
5
import { CacheHandlerValue } from 'next/dist/server/lib/incremental-cache' ;
6
+ import { pathToCacheKey } from 'srcCommon/cache-key' ;
6
7
7
8
// TODO: share XP response props with next-app for a proper type here
8
9
type XpResponseProps = Record < string , any > ;
@@ -31,24 +32,24 @@ const validateClientOptions = () => {
31
32
interface IRedisCache {
32
33
init ( buildId : string ) : Promise < IRedisCache > ;
33
34
34
- getRender ( key : string ) : Promise < CacheHandlerValue | null > ;
35
+ getRender ( path : string ) : Promise < CacheHandlerValue | null > ;
35
36
36
- setRender ( key : string , data : CacheHandlerValue ) : Promise < string | null > ;
37
+ setRender ( path : string , data : CacheHandlerValue ) : Promise < string | null > ;
37
38
38
- getResponse ( key : string ) : Promise < XpResponseProps | null > ;
39
+ getResponse ( path : string ) : Promise < XpResponseProps | null > ;
39
40
40
- setResponse ( key : string , data : XpResponseProps ) : Promise < string | null > ;
41
+ setResponse ( path : string , data : XpResponseProps ) : Promise < string | null > ;
41
42
42
- delete ( key : string ) : Promise < number > ;
43
+ delete ( path : string ) : Promise < number > ;
43
44
44
45
clear ( ) : Promise < string > ;
45
46
}
46
47
47
48
class RedisCacheImpl implements IRedisCache {
48
49
private readonly client : ReturnType < typeof createClient > ;
49
50
private readonly ttl : number = CACHE_TTL_24_HOURS_IN_MS ;
50
- private readonly responseCachePrefix = `${ process . env . ENV } :xp-response` ;
51
- private renderCachePrefix = '' ;
51
+ private readonly responseCacheKeyPrefix = `${ process . env . ENV } :xp-response` ;
52
+ private renderCacheKeyPrefix = '' ;
52
53
53
54
constructor ( ) {
54
55
this . client = createClient ( clientOptions )
@@ -70,70 +71,73 @@ class RedisCacheImpl implements IRedisCache {
70
71
}
71
72
72
73
public async init ( buildId : string ) {
73
- this . renderCachePrefix = `${ process . env . ENV } :render:${ buildId } ` ;
74
+ this . renderCacheKeyPrefix = `${ process . env . ENV } :render:${ buildId } ` ;
74
75
75
76
return this . client . connect ( ) . then ( ( ) => {
76
77
logger . info (
77
- `Initialized redis client with url ${ clientOptions . url } - TTL: ${ this . ttl } - Render prefix: ${ this . renderCachePrefix } - Response prefix: ${ this . responseCachePrefix } `
78
+ `Initialized redis client with url ${ clientOptions . url } - TTL: ${ this . ttl } - Render prefix: ${ this . renderCacheKeyPrefix } - Response prefix: ${ this . responseCacheKeyPrefix } `
78
79
) ;
79
80
return this ;
80
81
} ) ;
81
82
}
82
83
83
- private async get ( key : string ) {
84
+ private async get ( path : string ) {
84
85
return this . client
85
- . get ( key )
86
+ . get ( path )
86
87
. then ( ( result ) => ( result ? JSON . parse ( result ) : result ) )
87
88
. catch ( ( e ) => {
88
- logger . error ( `Error getting value for key ${ key } - ${ e } ` ) ;
89
+ logger . error ( `Error getting value for path ${ path } - ${ e } ` ) ;
89
90
return Promise . resolve ( null ) ;
90
91
} ) ;
91
92
}
92
93
93
- public async getRender ( key : string ) {
94
- return this . get ( this . getPrefixedKey ( key , this . renderCachePrefix ) ) ;
94
+ public async getRender ( path : string ) {
95
+ return this . get ( this . getCacheKey ( path , this . renderCacheKeyPrefix ) ) ;
95
96
}
96
97
97
- public async getResponse ( key : string ) {
98
- return this . get ( this . getPrefixedKey ( key , this . responseCachePrefix ) ) ;
98
+ public async getResponse ( path : string ) {
99
+ return this . get ( this . getCacheKey ( path , this . responseCacheKeyPrefix ) ) ;
99
100
}
100
101
101
- private async set < DataType > ( key : string , data : DataType ) {
102
+ private async set < DataType > ( path : string , data : DataType ) {
102
103
return this . client
103
- . set ( key , JSON . stringify ( data ) , {
104
+ . set ( path , JSON . stringify ( data ) , {
104
105
PX : this . ttl ,
105
106
} )
106
107
. then ( ( result ) => {
107
- logger . info ( `Redis set result for ${ key } : ${ result } ` ) ;
108
+ logger . info ( `Redis set result for ${ path } : ${ result } ` ) ;
108
109
return result ;
109
110
} )
110
111
. catch ( ( e ) => {
111
- logger . error ( `Error setting value for key ${ key } - ${ e } ` ) ;
112
+ logger . error ( `Error setting value for path ${ path } - ${ e } ` ) ;
112
113
return Promise . resolve ( null ) ;
113
114
} ) ;
114
115
}
115
116
116
- public async setRender ( key : string , data : CacheHandlerValue ) {
117
- return this . set ( this . getPrefixedKey ( key , this . renderCachePrefix ) , data ) ;
117
+ public async setRender ( path : string , data : CacheHandlerValue ) {
118
+ return this . set (
119
+ this . getCacheKey ( path , this . renderCacheKeyPrefix ) ,
120
+ data
121
+ ) ;
118
122
}
119
123
120
- public async setResponse ( key : string , data : XpResponseProps ) {
124
+ public async setResponse ( path : string , data : XpResponseProps ) {
121
125
return this . set (
122
- this . getPrefixedKey ( key , this . responseCachePrefix ) ,
126
+ this . getCacheKey ( path , this . responseCacheKeyPrefix ) ,
123
127
data
124
128
) ;
125
129
}
126
130
127
- public async delete ( key : string ) {
128
- const responseKey = this . getPrefixedKey ( key , this . responseCachePrefix ) ;
129
- const renderKey = this . getPrefixedKey ( key , this . renderCachePrefix ) ;
131
+ public async delete ( path : string ) {
132
+ const responseKey = this . getCacheKey ( path , this . responseCacheKeyPrefix ) ;
133
+ const renderKey = this . getCacheKey ( path , this . renderCacheKeyPrefix ) ;
130
134
131
135
logger . info (
132
136
`Deleting redis cache entries for ${ responseKey } and ${ renderKey } `
133
137
) ;
134
138
135
139
return this . client . del ( [ responseKey , renderKey ] ) . catch ( ( e ) => {
136
- logger . error ( `Error deleting value for key ${ key } - ${ e } ` ) ;
140
+ logger . error ( `Error deleting value for path ${ path } - ${ e } ` ) ;
137
141
return 0 ;
138
142
} ) ;
139
143
}
@@ -147,8 +151,8 @@ class RedisCacheImpl implements IRedisCache {
147
151
} ) ;
148
152
}
149
153
150
- private getPrefixedKey ( key : string , keyPrefix : string ) {
151
- return `${ keyPrefix } :${ key } ` ;
154
+ private getCacheKey ( path : string , keyPrefix : string ) {
155
+ return `${ keyPrefix } :${ pathToCacheKey ( path ) } ` ;
152
156
}
153
157
}
154
158
@@ -157,23 +161,23 @@ class RedisCacheDummy implements IRedisCache {
157
161
return this ;
158
162
}
159
163
160
- public async getRender ( key : string ) {
164
+ public async getRender ( path : string ) {
161
165
return null ;
162
166
}
163
167
164
- public async getResponse ( key : string ) {
168
+ public async getResponse ( path : string ) {
165
169
return null ;
166
170
}
167
171
168
- public async setRender ( key : string , data : CacheHandlerValue ) {
172
+ public async setRender ( path : string , data : CacheHandlerValue ) {
169
173
return null ;
170
174
}
171
175
172
- public async setResponse ( key : string , data : XpResponseProps ) {
176
+ public async setResponse ( path : string , data : XpResponseProps ) {
173
177
return null ;
174
178
}
175
179
176
- public async delete ( key : string ) {
180
+ public async delete ( path : string ) {
177
181
return 1 ;
178
182
}
179
183
0 commit comments