You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/nextjs-cache-handler/src/handlers/cache-handler.ts
+3-220Lines changed: 3 additions & 220 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,9 @@ import {
10
10
CacheHandlerParametersRevalidateTag,
11
11
CacheHandlerParametersSet,
12
12
CacheHandlerParametersGet,
13
+
Handler,
14
+
OnCreationHook,
15
+
Revalidate,
13
16
}from"./cache-handler.types";
14
17
import{PrerenderManifest}from"next/dist/build";
15
18
importtype{
@@ -19,228 +22,8 @@ import type {
19
22
GetIncrementalFetchCacheContext,
20
23
}from"next/dist/server/response-cache/types";
21
24
22
-
exporttypeRevalidate=false|number;
23
25
constPRERENDER_MANIFEST_VERSION=4;
24
26
25
-
/**
26
-
* Represents an internal Next.js metadata for a `get` method.
27
-
* This metadata is available in the `get` method of the cache handler.
28
-
*/
29
-
typeHandlerGetMeta={
30
-
/**
31
-
* An array of tags that are implicitly associated with the cache entry.
32
-
*/
33
-
implicitTags: string[];
34
-
};
35
-
36
-
/**
37
-
* Represents a cache Handler.
38
-
*/
39
-
exporttypeHandler={
40
-
/**
41
-
* A descriptive name for the cache Handler.
42
-
*/
43
-
name: string;
44
-
/**
45
-
* Retrieves the value associated with the given key from the cache.
46
-
*
47
-
* @param key - The unique string identifier for the cache entry.
48
-
*
49
-
* @param meta - The metadata object for the `get` method. See {@link HandlerGetMeta}.
50
-
*
51
-
* @returns A Promise that resolves to the cached value (if found), `null` or `undefined` if the entry is not found.
52
-
*
53
-
* @example
54
-
* ### With auto expiration
55
-
*
56
-
* If your cache store supports time based key eviction, the `get` method is straightforward.
57
-
*
58
-
* ```js
59
-
* const handler = {
60
-
* async get(key) {
61
-
* const cacheValue = await cacheStore.get(key);
62
-
*
63
-
* if (!cacheValue) {
64
-
* return null;
65
-
* }
66
-
*
67
-
* return cacheValue;
68
-
* }
69
-
* }
70
-
* ```
71
-
*
72
-
* ### Without auto expiration
73
-
*
74
-
* If your cache store does not support time based key eviction,
75
-
* you can implement the `delete` method to remove the cache entry when it becomes expired.
76
-
*
77
-
* ```js
78
-
* const handler = {
79
-
* async get(key) {
80
-
* const cacheValue = await cacheStore.get(key);
81
-
*
82
-
* if (!cacheValue) {
83
-
* return null;
84
-
* }
85
-
*
86
-
* return cacheValue;
87
-
* },
88
-
* async delete(key) {
89
-
* await cacheStore.delete(key);
90
-
* }
91
-
* }
92
-
* ```
93
-
*
94
-
95
-
*/
96
-
get: (
97
-
key: string,
98
-
meta: HandlerGetMeta,
99
-
)=>Promise<CacheHandlerValue|null|undefined>;
100
-
/**
101
-
* Sets or updates a value in the cache store.
102
-
*
103
-
* @param key - The unique string identifier for the cache entry.
104
-
*
105
-
* @param value - The value to be stored in the cache. See {@link CacheHandlerValue}.
106
-
*
107
-
* @returns A Promise that resolves when the value has been successfully set in the cache.
108
-
*
109
-
* @remarks
110
-
*
111
-
* Read more about the `lifespan` parameter: {@link LifespanParameters}.
112
-
*
113
-
* ### LifespanParameters
114
-
* If no `revalidate` option or `revalidate: false` is set in your [`getStaticProps` ↗](https://nextjs.org/docs/pages/api-reference/functions/get-static-props#revalidate)
115
-
* the `lifespan` parameter will be `null` and you should consider the cache entry as always fresh and never stale.
116
-
*
117
-
* Use the absolute time (`expireAt`) to set and expiration time for the cache entry in your cache store to be in sync with the file system cache.
* Deletes all cache entries that are associated with the specified tag.
124
-
* See [fetch `options.next.tags` and `revalidateTag` ↗](https://nextjs.org/docs/app/building-your-application/caching#fetch-optionsnexttags-and-revalidatetag)
125
-
*
126
-
* @param tag - A string representing the cache tag associated with the data you want to revalidate.
127
-
* Must be less than or equal to 256 characters. This value is case-sensitive.
128
-
*
129
-
130
-
*/
131
-
revalidateTag: (tag: string)=>Promise<void>;
132
-
/**
133
-
* Deletes the cache entry associated with the given key from the cache store.
134
-
* This method is optional and supposed to be used only when the cache store does not support time based key eviction.
135
-
* This method will be automatically called by the `CacheHandler` class when the retrieved cache entry is stale.
136
-
*
137
-
* @param key - The unique string identifier for the cache entry with prefix if present.
138
-
*
139
-
* @returns A Promise that resolves when the cache entry has been successfully deleted.
140
-
*/
141
-
delete?: (key: string)=>Promise<void>;
142
-
};
143
-
144
-
/**
145
-
* Represents the parameters for Time-to-Live (TTL) configuration.
146
-
*/
147
-
exporttypeTTLParameters={
148
-
/**
149
-
* The time in seconds for when the cache entry becomes stale.
150
-
*
151
-
* @default 31536000 // 1 year
152
-
*/
153
-
defaultStaleAge: number;
154
-
/**
155
-
* Estimates the expiration age based on the stale age.
156
-
*
157
-
* @param staleAge - The stale age in seconds.
158
-
* After the stale age, the cache entry is considered stale, can be served from the cache, and should be revalidated.
159
-
* Revalidation is handled by the `CacheHandler` class.
160
-
*
161
-
* @default (staleAge) => staleAge * 1.5
162
-
*
163
-
* @returns The expiration age in seconds.
164
-
*/
165
-
estimateExpireAge(staleAge: number): number;
166
-
};
167
-
168
-
/**
169
-
* Configuration options for the {@link CacheHandler}.
170
-
*/
171
-
exporttypeCacheHandlerConfig={
172
-
/**
173
-
* An array of cache instances that conform to the Handler interface.
174
-
* Multiple caches can be used to implement various caching strategies or layers.
175
-
*/
176
-
handlers: (Handler|undefined|null)[];
177
-
/**
178
-
* Time-to-live (TTL) options for the cache entries.
179
-
*/
180
-
ttl?: Partial<TTLParameters>;
181
-
};
182
-
183
-
/**
184
-
* Contextual information provided during cache creation, including server directory paths and environment mode.
185
-
*/
186
-
exporttypeCacheCreationContext={
187
-
/**
188
-
* The absolute path to the Next.js server directory.
189
-
*/
190
-
serverDistDir: string;
191
-
/**
192
-
* Indicates if the Next.js application is running in development mode.
193
-
* When in development mode, caching behavior might differ.
194
-
*/
195
-
dev?: boolean;
196
-
/**
197
-
* The unique identifier for the current build of the Next.js application.
198
-
* This build ID is generated during the `next build` process and is used
199
-
* to ensure consistency across multiple instances of the application,
200
-
* especially when running in containerized environments. It helps in
201
-
* identifying which version of the application is being served.
0 commit comments