@@ -3,31 +3,39 @@ import type { StorageValue } from 'axios-cache-interceptor';
33// eslint-disable-next-line import/no-extraneous-dependencies
44import { createClient } from 'redis' ;
55
6- const keyPrefix = 'axios-cache-' ;
6+ const KEY_PREFIX = 'axios-cache-' ;
7+
8+ const MIN_TTL = 60000 ;
79
810export default function createRedisStorage ( client : ReturnType < typeof createClient > ) {
911 // source https://axios-cache-interceptor.js.org/guide/storages#node-redis-storage
1012 return buildStorage ( {
1113 async find ( key ) {
12- const result = await client . get ( `${ keyPrefix } ${ key } ` ) ;
14+ const result = await client . get ( `${ KEY_PREFIX } ${ key } ` ) ;
1315 return result ? ( JSON . parse ( result ) as StorageValue ) : undefined ;
1416 } ,
1517
1618 // eslint-disable-next-line complexity
1719 async set ( key , value , req ) {
18- await client . set ( `${ keyPrefix } ${ key } ` , JSON . stringify ( value ) , {
20+ await client . set ( `${ KEY_PREFIX } ${ key } ` , JSON . stringify ( value ) , {
1921 PXAT :
22+ // We don't want to keep indefinitely values in the storage if
23+ // their request don't finish somehow. Either set its value as
24+ // the TTL or 1 minute (MIN_TTL).
2025 value . state === 'loading'
21- ? Date . now ( ) + ( req ?. cache && typeof req . cache . ttl === 'number' ? req . cache . ttl : 60000 )
26+ ? Date . now ( ) +
27+ ( req ?. cache && typeof req . cache . ttl === 'number' ? req . cache . ttl : MIN_TTL )
2228 : ( value . state === 'stale' && value . ttl ) ||
2329 ( value . state === 'cached' && ! canStale ( value ) )
2430 ? value . createdAt + value . ttl !
25- : undefined ,
31+ : // otherwise, we can't determine when it should expire, so we keep
32+ // it indefinitely.
33+ undefined ,
2634 } ) ;
2735 } ,
2836
2937 async remove ( key ) {
30- await client . del ( `${ keyPrefix } ${ key } ` ) ;
38+ await client . del ( `${ KEY_PREFIX } ${ key } ` ) ;
3139 } ,
3240 } ) ;
3341}
0 commit comments