-
Notifications
You must be signed in to change notification settings - Fork 166
Implement persistent data cache option and update cache key logic #920
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
Open
conico974
wants to merge
12
commits into
opennextjs:main
Choose a base branch
from
conico974:feat/persistent-data-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
925020c
compute cache key according to `persistentDataCache`
47c912e
remove build id from cache keys in overrides
b0236c8
fix for tag cache
3caf3b6
changeset
18583fe
fix linting
fd02cf1
create a cache key type
feb7aba
review fix
bc29e07
fix unit test
84d3634
review fix
732282a
tag cache refactored to use tagKey
fa4db57
fix linting
e036b68
fix unit test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@opennextjs/aws": minor | ||
--- | ||
|
||
Add an option to keep the data cache persistent between deployments. | ||
|
||
BREAKING CHANGE: Incremental cache keys are now an object of type `CacheKey` instead of a string. The new type includes properties like `baseKey`, `buildId`, and `cacheType`. Build_id is automatically provided according to the cache type and the `dangerous.persistentDataCache` option. Up to the Incremental Cache implementation to use it as they see fit. | ||
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.
|
||
**Custom Incremental cache will need to be updated** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,14 @@ import type { | |
IncrementalCacheContext, | ||
IncrementalCacheValue, | ||
} from "types/cache"; | ||
import { getTagsFromValue, hasBeenRevalidated, writeTags } from "utils/cache"; | ||
import type { CacheKey } from "types/overrides"; | ||
import { | ||
createCacheKey, | ||
createTagKey, | ||
getTagsFromValue, | ||
hasBeenRevalidated, | ||
writeTags, | ||
} from "utils/cache"; | ||
import { isBinaryContentType } from "../utils/binary"; | ||
import { debug, error, warn } from "./logger"; | ||
|
||
|
@@ -31,7 +38,7 @@ function isFetchCache( | |
// We need to use globalThis client here as this class can be defined at load time in next 12 but client is not available at load time | ||
export default class Cache { | ||
conico974 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public async get( | ||
key: string, | ||
baseKey: string, | ||
// fetchCache is for next 13.5 and above, kindHint is for next 14 and above and boolean is for earlier versions | ||
options?: | ||
| boolean | ||
|
@@ -50,14 +57,15 @@ export default class Cache { | |
const softTags = typeof options === "object" ? options.softTags : []; | ||
const tags = typeof options === "object" ? options.tags : []; | ||
return isFetchCache(options) | ||
? this.getFetchCache(key, softTags, tags) | ||
: this.getIncrementalCache(key); | ||
? this.getFetchCache(baseKey, softTags, tags) | ||
: this.getIncrementalCache(baseKey); | ||
} | ||
|
||
async getFetchCache(key: string, softTags?: string[], tags?: string[]) { | ||
debug("get fetch cache", { key, softTags, tags }); | ||
async getFetchCache(baseKey: string, softTags?: string[], tags?: string[]) { | ||
debug("get fetch cache", { baseKey, softTags, tags }); | ||
try { | ||
const cachedEntry = await globalThis.incrementalCache.get(key, "fetch"); | ||
const key = createCacheKey({ key: baseKey, type: "fetch" }); | ||
const cachedEntry = await globalThis.incrementalCache.get(key); | ||
|
||
if (cachedEntry?.value === undefined) return null; | ||
|
||
|
@@ -83,7 +91,7 @@ export default class Cache { | |
); | ||
if (path) { | ||
const hasPathBeenUpdated = await hasBeenRevalidated( | ||
path.replace("_N_T_/", ""), | ||
createCacheKey({ key: path.replace("_N_T_/", ""), type: "cache" }), | ||
[], | ||
cachedEntry, | ||
); | ||
|
@@ -105,9 +113,15 @@ export default class Cache { | |
} | ||
} | ||
|
||
async getIncrementalCache(key: string): Promise<CacheHandlerValue | null> { | ||
async getIncrementalCache( | ||
baseKey: string, | ||
): Promise<CacheHandlerValue | null> { | ||
try { | ||
const cachedEntry = await globalThis.incrementalCache.get(key, "cache"); | ||
const key = createCacheKey({ | ||
key: baseKey, | ||
type: "cache", | ||
}); | ||
const cachedEntry = await globalThis.incrementalCache.get(key); | ||
|
||
if (!cachedEntry?.value) { | ||
return null; | ||
|
@@ -191,118 +205,104 @@ export default class Cache { | |
} | ||
|
||
async set( | ||
key: string, | ||
baseKey: string, | ||
data?: IncrementalCacheValue, | ||
ctx?: IncrementalCacheContext, | ||
): Promise<void> { | ||
if (globalThis.openNextConfig.dangerous?.disableIncrementalCache) { | ||
return; | ||
} | ||
const key = createCacheKey({ | ||
key: baseKey, | ||
type: data?.kind === "FETCH" ? "fetch" : "cache", | ||
}); | ||
debug("Setting cache", { key, data, ctx }); | ||
// This one might not even be necessary anymore | ||
// Better be safe than sorry | ||
const detachedPromise = globalThis.__openNextAls | ||
.getStore() | ||
?.pendingPromiseRunner.withResolvers<void>(); | ||
try { | ||
if (data === null || data === undefined) { | ||
await globalThis.incrementalCache.delete(key); | ||
// only case where we delete the cache is for ISR/SSG cache | ||
await globalThis.incrementalCache.delete(key as CacheKey<"cache">); | ||
} else { | ||
const revalidate = this.extractRevalidateForSet(ctx); | ||
switch (data.kind) { | ||
case "ROUTE": | ||
case "APP_ROUTE": { | ||
const { body, status, headers } = data; | ||
await globalThis.incrementalCache.set( | ||
key, | ||
{ | ||
type: "route", | ||
body: body.toString( | ||
isBinaryContentType(String(headers["content-type"])) | ||
? "base64" | ||
: "utf8", | ||
), | ||
meta: { | ||
status, | ||
headers, | ||
}, | ||
revalidate, | ||
await globalThis.incrementalCache.set(key, { | ||
type: "route", | ||
body: body.toString( | ||
isBinaryContentType(String(headers["content-type"])) | ||
? "base64" | ||
: "utf8", | ||
), | ||
meta: { | ||
status, | ||
headers, | ||
}, | ||
"cache", | ||
); | ||
revalidate, | ||
}); | ||
break; | ||
} | ||
case "PAGE": | ||
case "PAGES": { | ||
const { html, pageData, status, headers } = data; | ||
const isAppPath = typeof pageData === "string"; | ||
if (isAppPath) { | ||
await globalThis.incrementalCache.set( | ||
key, | ||
{ | ||
type: "app", | ||
html, | ||
rsc: pageData, | ||
meta: { | ||
status, | ||
headers, | ||
}, | ||
revalidate, | ||
}, | ||
"cache", | ||
); | ||
} else { | ||
await globalThis.incrementalCache.set( | ||
key, | ||
{ | ||
type: "page", | ||
html, | ||
json: pageData, | ||
revalidate, | ||
}, | ||
"cache", | ||
); | ||
} | ||
break; | ||
} | ||
case "APP_PAGE": { | ||
const { html, rscData, headers, status } = data; | ||
await globalThis.incrementalCache.set( | ||
key, | ||
{ | ||
await globalThis.incrementalCache.set(key, { | ||
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. Look like this is a formatting only diff? |
||
type: "app", | ||
html, | ||
rsc: rscData.toString("utf8"), | ||
rsc: pageData, | ||
meta: { | ||
status, | ||
headers, | ||
}, | ||
revalidate, | ||
}); | ||
} else { | ||
await globalThis.incrementalCache.set(key, { | ||
type: "page", | ||
html, | ||
json: pageData, | ||
revalidate, | ||
}); | ||
} | ||
break; | ||
} | ||
case "APP_PAGE": { | ||
const { html, rscData, headers, status } = data; | ||
await globalThis.incrementalCache.set(key, { | ||
type: "app", | ||
html, | ||
rsc: rscData.toString("utf8"), | ||
meta: { | ||
status, | ||
headers, | ||
}, | ||
"cache", | ||
); | ||
revalidate, | ||
}); | ||
break; | ||
} | ||
case "FETCH": | ||
await globalThis.incrementalCache.set(key, data, "fetch"); | ||
await globalThis.incrementalCache.set(key, data); | ||
break; | ||
case "REDIRECT": | ||
await globalThis.incrementalCache.set( | ||
key, | ||
{ | ||
type: "redirect", | ||
props: data.props, | ||
revalidate, | ||
}, | ||
"cache", | ||
); | ||
await globalThis.incrementalCache.set(key, { | ||
type: "redirect", | ||
props: data.props, | ||
revalidate, | ||
}); | ||
break; | ||
case "IMAGE": | ||
// Not implemented | ||
break; | ||
} | ||
} | ||
|
||
await this.updateTagsOnSet(key, data, ctx); | ||
await this.updateTagsOnSet(baseKey, data, ctx); | ||
debug("Finished setting cache"); | ||
} catch (e) { | ||
error("Failed to set cache", e); | ||
|
@@ -324,7 +324,10 @@ export default class Cache { | |
|
||
try { | ||
if (globalThis.tagCache.mode === "nextMode") { | ||
const paths = (await globalThis.tagCache.getPathsByTags?.(_tags)) ?? []; | ||
const paths = | ||
(await globalThis.tagCache.getPathsByTags?.( | ||
_tags.map(createTagKey), | ||
)) ?? []; | ||
|
||
await writeTags(_tags); | ||
if (paths.length > 0) { | ||
|
@@ -350,7 +353,7 @@ export default class Cache { | |
for (const tag of _tags) { | ||
debug("revalidateTag", tag); | ||
// Find all keys with the given tag | ||
const paths = await globalThis.tagCache.getByTag(tag); | ||
const paths = await globalThis.tagCache.getByTag(createTagKey(tag)); | ||
debug("Items", paths); | ||
const toInsert = paths.map((path) => ({ | ||
path, | ||
|
@@ -361,11 +364,15 @@ export default class Cache { | |
if (tag.startsWith("_N_T_/")) { | ||
for (const path of paths) { | ||
// We need to find all hard tags for a given path | ||
const _tags = await globalThis.tagCache.getByPath(path); | ||
const _tags = await globalThis.tagCache.getByPath( | ||
createTagKey(path), | ||
); | ||
const hardTags = _tags.filter((t) => !t.startsWith("_N_T_/")); | ||
// For every hard tag, we need to find all paths and revalidate them | ||
for (const hardTag of hardTags) { | ||
const _paths = await globalThis.tagCache.getByTag(hardTag); | ||
const _paths = await globalThis.tagCache.getByTag( | ||
createTagKey(hardTag), | ||
); | ||
debug({ hardTag, _paths }); | ||
toInsert.push( | ||
..._paths.map((path) => ({ | ||
|
@@ -378,7 +385,12 @@ export default class Cache { | |
} | ||
|
||
// Update all keys with the given tag with revalidatedAt set to now | ||
await writeTags(toInsert); | ||
await writeTags( | ||
toInsert.map((t) => ({ | ||
path: createTagKey(t.path), | ||
tag: createTagKey(t.tag), | ||
})), | ||
); | ||
|
||
// We can now invalidate all paths in the CDN | ||
// This only applies to `revalidateTag`, not to `res.revalidate()` | ||
|
@@ -439,13 +451,13 @@ export default class Cache { | |
|
||
// Get all tags stored in dynamodb for the given key | ||
// If any of the derived tags are not stored in dynamodb for the given key, write them | ||
const storedTags = await globalThis.tagCache.getByPath(key); | ||
const storedTags = await globalThis.tagCache.getByPath(createTagKey(key)); | ||
const tagsToWrite = derivedTags.filter((tag) => !storedTags.includes(tag)); | ||
if (tagsToWrite.length > 0) { | ||
await writeTags( | ||
tagsToWrite.map((tag) => ({ | ||
path: key, | ||
tag: tag, | ||
path: createTagKey(key), | ||
tag: createTagKey(tag), | ||
// In case the tags are not there we just need to create them | ||
// but we don't want them to return from `getLastModified` as they are not stale | ||
revalidatedAt: 1, | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.