1
-
2
1
# Next-Cache-Handler
3
2
4
3
## Introduction
@@ -35,21 +34,25 @@ yarn add @dbbs/next-cache-handler-core
35
34
36
35
### Configuration
37
36
Create a ` cacheHandler.js ` in your project and configure as follows:
38
- ``` javascript
39
- import { Cache , FileSystemCache } from ' @dbbs/next-cache-handler-core'
37
+ ``` typescript
38
+ import { Cache , FileSystemCache , ConsoleLogger } from ' @dbbs/next-cache-handler-core'
39
+
40
+ const config = {
41
+ cacheCookies: [' my-cookie-1' , ' my-cookie-2' ],
42
+ cacheQueries: [' my-query-1' , ' my-query-2' ],
43
+ enableDeviceSplit: true ,
44
+ noCacheMatchers: [],
45
+ cache: new FileSystemCache (),
46
+ logger: new ConsoleLogger ()
47
+ }
40
48
41
- Cache
42
- .setCacheStrategy (new FileSystemCache ())
43
- .addCookies ([' my-cookie-1' ,' my-cookie-2' ])
44
- .addQueries ([' my-query-1' ,' my-query-2' ])
45
- .addDeviceSplit ()
46
- // Additional configuration...
49
+ Cache .setConfig (config )
47
50
export default Cache
48
51
```
49
52
50
53
Update your ` next.config.js ` to use the cache handler.
51
54
52
- ```
55
+ ``` typescript
53
56
const nextConfig = {
54
57
cacheHandler: require .resolve (' ./cacheHandler.js' )
55
58
}
@@ -60,12 +63,12 @@ export default nextConfig
60
63
## Cache Strategies
61
64
Explore various caching strategies to find the best fit for your application's needs:
62
65
- ** MemoryCache** : Ideal for short-lived data in applications with limited cache size requirements. By default limit of size is set to 512MB.
63
- ```
66
+ ``` typescript
64
67
import { Cache , MemoryCache } from ' @dbbs/next-cache-handler-core'
65
68
66
69
Cache .setCacheStrategy (new MemoryCache ())
67
70
```
68
- ```
71
+ ``` typescript
69
72
import { Cache , MemoryCache } from ' @dbbs/next-cache-handler-core'
70
73
71
74
// extending size limitation to 1024MB.
@@ -78,7 +81,7 @@ import { Cache, FileSystemCache } from '@dbbs/next-cache-handler-core'
78
81
Cache.setCacheStrategy(new FileSystemCache())
79
82
```
80
83
- ** Redis** : Perfect for distributed applications requiring shared cache access.
81
- ```
84
+ ``` typescript
82
85
import { Cache } from ' @dbbs/next-cache-handler-core'
83
86
import { RedisCache } from ' @dbbs/next-cache-handler-redis'
84
87
@@ -94,7 +97,7 @@ Cache.setCacheStrategy(new RedisCache(redisConnectionOpts))
94
97
By default, we use plain Redis as the cache store.
95
98
But you are able to use Redis with RedisJSON and RedisSearch modules as the cache store.
96
99
In that case you need to pass extra params to RedisCache class during initialization.
97
- ```
100
+ ``` typescript
98
101
import { Cache } from ' @dbbs/next-cache-handler-core'
99
102
import { RedisCache , RedisStrategy } from " @dbbs/next-cache-handler-redis"
100
103
@@ -104,56 +107,54 @@ const redisConnectionOpts = {
104
107
...
105
108
}
106
109
107
- Cache.setCacheStrategy(new RedisCache(redisConnectionOpts, RedisStrategy.RedisStack))))
110
+ Cache .setConfig ({
111
+ ... config ,
112
+ cache: new RedisCache (redisConnectionOpts , RedisStrategy .RedisJSON ),
113
+ })
108
114
```
109
115
- ** S3** : Suitable for high scalable applications.
110
- ```
116
+ ``` typescript
111
117
import { Cache } from ' @dbbs/next-cache-handler-core'
112
118
import { S3Cache } from ' @dbbs/next-cache-handler-s3'
113
119
114
- Cache.setCacheStrategy(new S3Cache('cache-bucket-name'))
120
+ Cache .setConfig ({
121
+ ... config ,
122
+ cache: new S3Cache (' cache-bucket-name' ),
123
+ })
115
124
```
116
125
117
126
## API Reference
118
127
119
- ### ` addCookies `
120
- Cookie names what is going to be used to fragmentate cache based on browser cookies session.
121
- ```
122
- Cache.addCookies(['my-cookie-to-split'])
123
- ```
128
+ ### Configuration Options
129
+ The cache handler accepts the following configuration options:
124
130
125
- ### ` addQueries `
126
- Query names to fragmentate cache based on url query parameters.
127
- ```
128
- Cache.addQueries(['my-query-to-split'])
129
- ```
131
+ - ` cacheCookies ` : Array of cookie names to use for cache segmentation
132
+ - ` cacheQueries ` : Array of query parameter names to use for cache segmentation
133
+ - ` enableDeviceSplit ` : Boolean to enable/disable device-based cache segmentation
134
+ - ` noCacheMatchers ` : Array of path patterns to exclude from caching
135
+ - ` cache ` : Cache strategy instance (FileSystemCache, MemoryCache, etc.)
136
+ - ` logger ` : Logger instance implementing the BaseLogger interface
130
137
131
- ### ` addDeviceSplit `
132
- Enables to fragmentate experience based on current ` User-Agent ` of the request.
133
- ```
134
- Cache.addDeviceSplit()
135
- ```
138
+ ``` typescript
139
+ import { Cache , FileSystemCache } from ' @dbbs/next-cache-handler-core'
136
140
137
- ### ` setCacheStrategy `
138
- Applies given strategy to cache page data.
139
- ```
140
- Cache.setCacheStrategy(new MemoryCache())
141
- ```
141
+ const config = {
142
+ cacheCookies: [' my-cookie-1' , ' my-cookie-2' ],
143
+ cacheQueries: [' my-query-1' , ' my-query-2' ],
144
+ enableDeviceSplit: true ,
145
+ noCacheMatchers: [],
146
+ cache: new FileSystemCache (),
147
+ logger: new ConsoleLogger ()
148
+ }
142
149
143
- ### ` addNoCacheMatchers `
144
- This allows you to exclude pages for which caching should be disabled.
145
- You can match one or more paths using array syntax. The matcher config allows full regex.
146
- Add route to ignore cache list. All routes added here would be excluded from caching and will always render again.
147
- ```
148
- Cache.addNoCacheMatchers('/home')
149
- Cache.addNoCacheMatchers(['/home','/about'])
150
- Cache.addNoCacheMatchers('/catalog/:page')
150
+ Cache .setConfig (config )
151
+
152
+ export default Cache
151
153
```
152
- Read more details on [ path-to-regexp] ( https://github.com/pillarjs/path-to-regexp#path-to-regexp-1 ) documentation.
153
154
154
155
## Logging
155
156
Leverage the built-in logger for monitoring cache operations or integrate your custom logger for advanced logging capabilities.
156
- ```
157
+ ``` typescript
157
158
import { BaseLogger , Cache , FileSystemCache } from ' @dbbs/next-cache-handler-core'
158
159
159
160
class MyCustomLogger implements BaseLogger {
0 commit comments