1
1
const { IncrementalCache } = require ( '@neshca/cache-handler' ) ;
2
+ const { createHandler } = require ( '@neshca/cache-handler/redis-stack' ) ;
2
3
const { createClient } = require ( 'redis' ) ;
3
4
4
5
if ( ! process . env . REDIS_URL ) {
5
6
console . warn ( 'Make sure that REDIS_URL is added to the .env.local file and loaded properly.' ) ;
6
7
}
7
8
8
- /** @type {import('@neshca/cache-handler').TagsManifest } */
9
- let localTagsManifest = {
10
- version : 1 ,
11
- items : { } ,
12
- } ;
13
-
14
9
const PREFIX = 'JSON:' ;
15
- const TAGS_MANIFEST_KEY = `${ PREFIX } sharedTagsManifest` ;
16
10
const CONNECT_TIMEOUT_MS = 5 * 50 * 1000 ;
17
11
18
12
function createRedisClient ( url ) {
@@ -31,73 +25,27 @@ function createRedisClient(url) {
31
25
return client ;
32
26
}
33
27
34
- async function connectAndSetManifest ( client ) {
28
+ async function connect ( client ) {
35
29
try {
36
30
await client . connect ( ) ;
37
31
} catch ( error ) {
38
32
console . error ( 'Redis connection error:' , error . message ) ;
39
33
}
40
-
41
- try {
42
- await client . json . set ( TAGS_MANIFEST_KEY , '.' , localTagsManifest , { NX : true } ) ;
43
- } catch ( error ) {
44
- console . error ( 'Redis set tagsManifest error:' , error . message ) ;
45
- }
46
34
}
47
35
48
36
if ( process . env . SERVER_STARTED ) {
49
37
const client = createRedisClient ( process . env . REDIS_URL ) ;
50
38
51
- connectAndSetManifest ( client ) . then ( ( ) => {
39
+ connect ( client ) . then ( ( ) => {
52
40
console . log ( 'Redis connected' ) ;
53
41
} ) ;
54
42
55
- IncrementalCache . onCreation ( ( ) => {
56
- return {
57
- cache : {
58
- async get ( key ) {
59
- try {
60
- const value = ( await client . json . get ( PREFIX + key ) ) ?? null ;
61
-
62
- if ( value && value . kind === 'ROUTE' && value . body . type === 'Buffer' ) {
63
- value . body = Buffer . from ( value . body ) ;
64
- }
65
-
66
- return value ;
67
- } catch ( error ) {
68
- return null ;
69
- }
70
- } ,
71
- async set ( key , value ) {
72
- try {
73
- await client . json . set ( PREFIX + key , '.' , value ) ;
74
- } catch ( error ) {
75
- // ignore because value will be written to disk
76
- }
77
- } ,
78
- async getTagsManifest ( ) {
79
- try {
80
- const sharedTagsManifest = ( await client . json . get ( TAGS_MANIFEST_KEY ) ) ?? null ;
81
-
82
- if ( sharedTagsManifest ) {
83
- localTagsManifest = sharedTagsManifest ;
84
- }
85
-
86
- return sharedTagsManifest ;
87
- } catch ( error ) {
88
- return localTagsManifest ;
89
- }
90
- } ,
91
- async revalidateTag ( tag , revalidatedAt ) {
92
- try {
93
- await client . json . set ( TAGS_MANIFEST_KEY , `.items.${ tag } ` , { revalidatedAt } ) ;
94
- } catch ( error ) {
95
- localTagsManifest . items [ tag ] = { revalidatedAt } ;
96
- }
97
- } ,
98
- } ,
99
- } ;
100
- } ) ;
43
+ IncrementalCache . onCreation (
44
+ createHandler ( {
45
+ client,
46
+ keyPrefix : PREFIX ,
47
+ } ) ,
48
+ ) ;
101
49
}
102
50
103
51
module . exports = IncrementalCache ;
0 commit comments