1
1
import FileSystemCache from 'next/dist/server/lib/incremental-cache/file-system-cache' ;
2
2
import { LRUCache } from 'lru-cache' ;
3
3
import { CacheHandlerValue } from 'next/dist/server/lib/incremental-cache' ;
4
- import { RedisCache , RedisCacheDummy } from 'cache/redis' ;
5
- import { isLeaderPod } from 'leader-pod' ;
6
-
7
- const CACHE_TTL_24_HOURS_IN_MS = 3600 * 24 * 1000 ;
4
+ import { RedisCache } from 'srcCommon/redis' ;
5
+ import { CACHE_TTL_72_HOURS_IN_MS } from 'srcCommon/constants' ;
6
+ import { pathToCacheKey } from 'srcCommon/cache-key' ;
8
7
9
8
const TTL_RESOLUTION_MS = 60 * 1000 ;
10
9
11
- export const redisCache = ! process . env . REDIS_URI_PAGECACHE
12
- ? new RedisCacheDummy ( )
13
- : new RedisCache ( { ttl : CACHE_TTL_24_HOURS_IN_MS } ) ;
10
+ export const redisCache = new RedisCache ( ) ;
14
11
15
12
const localCache = new LRUCache < string , CacheHandlerValue > ( {
16
13
max : 2000 ,
17
- ttl : CACHE_TTL_24_HOURS_IN_MS ,
14
+ ttl : CACHE_TTL_72_HOURS_IN_MS ,
18
15
ttlResolution : TTL_RESOLUTION_MS ,
19
16
} ) ;
20
17
@@ -27,16 +24,16 @@ export default class PageCacheHandler {
27
24
return fromLocalCache ;
28
25
}
29
26
30
- const fromRedisCache = await redisCache . get ( key ) ;
27
+ const fromRedisCache = await redisCache . getRender ( key ) ;
31
28
if ( ! fromRedisCache ) {
32
29
return null ;
33
30
}
34
31
35
32
const ttlRemaining = fromRedisCache . lastModified
36
33
? fromRedisCache . lastModified +
37
- CACHE_TTL_24_HOURS_IN_MS -
34
+ CACHE_TTL_72_HOURS_IN_MS -
38
35
Date . now ( )
39
- : CACHE_TTL_24_HOURS_IN_MS ;
36
+ : CACHE_TTL_72_HOURS_IN_MS ;
40
37
41
38
if ( ttlRemaining > TTL_RESOLUTION_MS ) {
42
39
localCache . set ( key , fromRedisCache , {
@@ -56,23 +53,14 @@ export default class PageCacheHandler {
56
53
} ;
57
54
58
55
localCache . set ( key , cacheItem ) ;
59
- redisCache . set ( key , cacheItem ) ;
56
+ redisCache . setRender ( key , cacheItem ) ;
60
57
}
61
58
62
59
public async clear ( ) {
63
60
localCache . clear ( ) ;
64
-
65
- if ( await isLeaderPod ( ) ) {
66
- return redisCache . clear ( ) ;
67
- }
68
61
}
69
62
70
63
public async delete ( path : string ) {
71
- const pagePath = path === '/' ? '/index' : path ;
72
- localCache . delete ( pagePath ) ;
73
-
74
- if ( await isLeaderPod ( ) ) {
75
- return redisCache . delete ( pagePath ) ;
76
- }
64
+ localCache . delete ( pathToCacheKey ( path ) ) ;
77
65
}
78
66
}
0 commit comments