-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add non-destructive cache hydration support via isInitialHydrat… #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,7 +99,9 @@ export default function createHandler({ | |
|
|
||
| return Promise.resolve(cacheValue); | ||
| }, | ||
| set(key, cacheHandlerValue) { | ||
| set(key, cacheHandlerValue, ctx) { | ||
| // LRU cache is in-memory and ephemeral, so NX behavior is not applicable | ||
| // We always set the value regardless of isInitialHydration flag | ||
|
Comment on lines
+103
to
+104
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NX is a redis specific acronym, right? It shouldn't be mentioned in LRU implementation. You could also call |
||
| lruCacheStore.set(key, cacheHandlerValue); | ||
|
|
||
| return Promise.resolve(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,7 +221,7 @@ export default function createHandler({ | |
|
|
||
| return cacheValue; | ||
| }, | ||
| async set(key, cacheHandlerValue) { | ||
| async set(key, cacheHandlerValue, ctx) { | ||
| assertClientIsReady(); | ||
|
|
||
| let setOperation: Promise<string | null>; | ||
|
|
@@ -260,23 +260,27 @@ export default function createHandler({ | |
|
|
||
| switch (keyExpirationStrategy) { | ||
| case "EXAT": { | ||
| const setOptions = | ||
| typeof lifespan?.expireAt === "number" | ||
| ? { | ||
| EXAT: lifespan.expireAt, | ||
| ...(ctx?.isInitialHydration ? { NX: true } : {}), | ||
| } | ||
| : ctx?.isInitialHydration | ||
| ? { NX: true } | ||
| : undefined; | ||
|
Comment on lines
+263
to
+271
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| setOperation = client | ||
| .withAbortSignal(AbortSignal.timeout(timeoutMs)) | ||
| .set( | ||
| keyPrefix + key, | ||
| serializedValue, | ||
| typeof lifespan?.expireAt === "number" | ||
| ? { | ||
| EXAT: lifespan.expireAt, | ||
| } | ||
| : undefined, | ||
| ); | ||
| .set(keyPrefix + key, serializedValue, setOptions); | ||
| break; | ||
| } | ||
| case "EXPIREAT": { | ||
| const setOptions = ctx?.isInitialHydration ? { NX: true } : undefined; | ||
|
|
||
| setOperation = client | ||
| .withAbortSignal(AbortSignal.timeout(timeoutMs)) | ||
| .set(keyPrefix + key, serializedValue); | ||
| .set(keyPrefix + key, serializedValue, setOptions); | ||
|
|
||
| expireOperation = lifespan | ||
| ? client | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -236,6 +236,7 @@ export async function registerInitialCache( | |
| revalidate, | ||
| internal_lastModified: lastModified, | ||
| tags: getTagsFromHeaders(meta.headers), | ||
| isInitialHydration: true, | ||
| }); | ||
| } catch (error) { | ||
| if (debug) { | ||
|
|
@@ -376,6 +377,7 @@ export async function registerInitialCache( | |
| await cacheHandler.set(cachePath, value, { | ||
| revalidate, | ||
| internal_lastModified: lastModified, | ||
| isInitialHydration: true, | ||
| }); | ||
|
|
||
| if (debug) { | ||
|
|
@@ -490,6 +492,7 @@ export async function registerInitialCache( | |
| revalidate, | ||
| internal_lastModified: lastModified, | ||
| tags: fetchCache.tags, | ||
| isInitialHydration: true, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add this as a feature flag in We could call it |
||
| }); | ||
| } catch (error) { | ||
| if (debug) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps
setOnlyIfNotExists? I thinkisInitialHydrationis a breaking change that you are forced to opt in in hydration. You should rather to have a choice to pick a strategy.