File tree Expand file tree Collapse file tree 1 file changed +20
-11
lines changed Expand file tree Collapse file tree 1 file changed +20
-11
lines changed Original file line number Diff line number Diff line change @@ -160,6 +160,11 @@ const cacheResponse = (options = {}) => {
160
160
const oldSend = res . send ;
161
161
162
162
res . send = ( body ) => {
163
+ if ( res . statusCode < 200 || res . statusCode >= 300 ) {
164
+ res . send = oldSend ;
165
+ return res . send ( body ) ;
166
+ }
167
+
163
168
const cacheValue = {
164
169
priority : priority ,
165
170
response : body ,
@@ -206,19 +211,23 @@ const invalidateCache = (options = {}) => {
206
211
}
207
212
208
213
return async ( req , res , next ) => {
209
- try {
210
- for ( const key of keys ) {
211
- const cachedKeysSet = cachedKeys . getCachedKeys ( key ) ;
212
- for ( const cachedKey of cachedKeysSet ) {
213
- pool . evict ( cachedKey ) ;
214
+ res . on ( "finish" , ( ) => {
215
+ if ( res . statusCode < 200 || res . statusCode >= 300 ) {
216
+ return ;
217
+ }
218
+ try {
219
+ for ( const key of keys ) {
220
+ const cachedKeysSet = cachedKeys . getCachedKeys ( key ) ;
221
+ for ( const cachedKey of cachedKeysSet ) {
222
+ pool . evict ( cachedKey ) ;
223
+ }
224
+ cachedKeys . removeModelKey ( key ) ;
214
225
}
215
- cachedKeys . removeModelKey ( key ) ;
226
+ } catch ( err ) {
227
+ logger . error ( `Error while removing cached response ${ err } ` ) ;
216
228
}
217
- } catch ( err ) {
218
- logger . error ( `Error while removing cached response ${ err } ` ) ;
219
- } finally {
220
- next ( ) ;
221
- }
229
+ } ) ;
230
+ next ( ) ;
222
231
} ;
223
232
} ;
224
233
You can’t perform that action at this time.
0 commit comments