-
Notifications
You must be signed in to change notification settings - Fork 73
Hash cache keys to limit their length #553
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
Conversation
|
commit: |
export function computeCacheKey(key: string, options: KeyOptions) { | ||
const { isFetch, directory, buildId } = options; | ||
const hash = createHash("sha256"); | ||
return `${directory}/${buildId}/${hash.update(key).digest("hex")}.${isFetch ? "fetch" : "cache"}`.replace( |
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.
Question: we have a directory for R2 but not for KV.
Should we unify that? if yes by adding or removing the directory?
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.
I think the only point in having a directory is if people want to use the same bucket for other things than just Next cache.
In this case it makes sense to add it to KV as well.
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.
I think the directory is a nice feature 👍
When moving to R2 today, it was useful to easily navigate the cache from the R2 dashboard.
It doesn't look like it'll be as useful in KV, but to stay consistent, I think it makes sense to use /
.
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.
Ideally there would be a single function for computing the cache key
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.
@SamyPesse Thanks for your feedback!
Can I ask how it is helpful? Do you store other objects than cache in the same bucket?
I think it makes sense to use /
What do you mean by "use /"?
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.
R2 has a directory as people are likely to want to re-use the same object storage across multiple apps and so it can namespace each app.
Agreed it would be nice to have a single function providing a consistent way to create cache keys across adapters.
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.
people are likely to want to re-use the same object storage across multiple apps
Why is that?
I was thinking about deleting R2/KV before populating to avoid increase the storage space for each new deployment - in which case we would not really need a directory
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.
people are likely to want to re-use the same object storage across multiple apps
Why is that?
If an org has 10 microfrontends for their website (not unrealistic), i view it as less overhead/complexity to have a single object storage bucket managing the data/route cache across apps than having separate buckets for each app. Realistically, you would never have one object storage per app on a platform - the data is segmented within that store by each app, but across the platform you'd use the same underlying data store. I expect Vercel's cache to have an object storage instance with lots of different projects, and cache entries segmented by projects for example.
I was thinking about deleting R2/KV before populating to avoid increase the storage space for each new deployment - in which case we would not really need a directory
The deployment currently in production would still depend on having access to that data in R2/KV before the next deployment happens as population is pre-deployment, so I'm not sure I understand how that would work without unintended side effects. Ideally it would be a post-deployment cleanup action for old builds.
Even post-deployment is a sticky situation as applications may still point some traffic to old builds to avoid version skew.
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.
i view it as less overhead/complexity
I don't really get the complexity difference of a bucket name vs a directory.
The deployment currently in production would still depend on having access to that data in R2/KV ...
That's true.
On the other hand cleanup is not really possible today.
It would means batch listing + deleting.
First it is not possible to list given a prefix.
Second deleting entries one by one incurs $$$.
Something that we need to solve.
export function computeCacheKey(key: string, options: KeyOptions) { | ||
const { isFetch, directory, buildId } = options; | ||
const hash = createHash("sha256"); | ||
return `${directory}/${buildId}/${hash.update(key).digest("hex")}.${isFetch ? "fetch" : "cache"}`.replace( |
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.
I think the only point in having a directory is if people want to use the same bucket for other things than just Next cache.
In this case it makes sense to add it to KV as well.
export function computeCacheKey(key: string, options: KeyOptions) { | ||
const { isFetch, directory, buildId } = options; | ||
const hash = createHash("sha256"); | ||
return `${directory}/${buildId}/${hash.update(key).digest("hex")}.${isFetch ? "fetch" : "cache"}`.replace( |
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.
Ideally there would be a single function for computing the cache key
const relativePath = path.relative(path.join(opts.outputDir, "cache"), fullPath); | ||
|
||
if (relativePath.startsWith("__fetch")) { | ||
const [__fetch, buildId, ...keyParts] = relativePath.split("/"); |
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.
Do we need keyParts
to be an array for fetch ? (i haven't checked, i might be wrong)
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.
The Next.js fetch key is a sha-256 - so the array is not stricly needed but it doesn't hurt and makes code consistent.
packages/cloudflare/src/api/overrides/incremental-cache/kv-incremental-cache.ts
Show resolved
Hide resolved
packages/cloudflare/src/api/overrides/incremental-cache/r2-incremental-cache.ts
Show resolved
Hide resolved
export function computeCacheKey(key: string, options: KeyOptions) { | ||
const { isFetch, directory, buildId } = options; | ||
const hash = createHash("sha256"); | ||
return `${directory}/${buildId}/${hash.update(key).digest("hex")}.${isFetch ? "fetch" : "cache"}`.replace( |
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.
R2 has a directory as people are likely to want to re-use the same object storage across multiple apps and so it can namespace each app.
Agreed it would be nice to have a single function providing a consistent way to create cache keys across adapters.
1035fd9
to
f49f9d5
Compare
I have addressed the feedback and created #556 to add directory to KV |
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.
LGTM
fixes #550
⧓ Review in Butler Review
#4OeVF3V8a
vicb/hash-cache-key
7 commit series (version 1)
Please leave review feedback in the Butler Review