Skip to content

Feat Automatic cache purge #655

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Feat Automatic cache purge #655

wants to merge 9 commits into from

Conversation

conico974
Copy link
Collaborator

This PR will allow to automatically purge Cache Api entry everywhere where we use them

Copy link

changeset-bot bot commented May 8, 2025

⚠️ No Changeset found

Latest commit: 12ca679

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

pkg-pr-new bot commented May 8, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/cloudflare@655

commit: 12ca679

@conico974 conico974 marked this pull request as ready for review May 9, 2025 14:27
Copy link
Collaborator Author

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure about naming here. If you have better ideas

`,
tags.map((row) => row.tag)
);
if (tags.length < 100) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (tags.length < 100) {
if (tags.length < MAX_NUMBER_OF_TAGS_PER_PURGE) {

tags.map((row) => row.tag)
);
if (tags.length < 100) {
// If we have less than 100 tags, we can stop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If we have less than 100 tags, we can stop
// If we have less than MAX_NUMBER_OF_TAGS_PER_PURGE tags, we can stop

Comment on lines 46 to 57
let tags = this.ctx.storage.sql
.exec<{ tag: string }>(
`
SELECT * FROM cache_purge LIMIT ${MAX_NUMBER_OF_TAGS_PER_PURGE}
`
)
.toArray();
if (tags.length === 0) {
// No tags to purge, we can stop
// It shouldn't happen, but just in case
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can move this at the beginning of the loop and adjust the while condition to avoid duplication?

// We should use it
if (env.NEXT_CACHE_DO_PURGE) {
const durableObject = env.NEXT_CACHE_DO_PURGE;
if (!durableObject) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should always be defined (check on l38)

tags.map((row) => row.tag)
);
// Delete the tags from the sql table
this.ctx.storage.sql.exec(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to delete only if internalPurgeCacheByTags was successful?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could do that, but it means we have to keep every single tag, it doesn't look like we can know if it only fails partially

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually keeping them might cause an issue, we could end up in a loop trying to purge something that somehow can't get purged. And if it happens it could end up blocking a lot of other tag from being invalidated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the cache purge API:

When there is an error, it could 200 with:

{
  success: false,
  errors: [
    {
      code: 1100,
      message: 'Tag exceeds maximum length of 1024 characters: ....'
    },
    { code: 1016, message: 'One or more cache purge errors' }
  ],
  messages: [],
  result: null
}

{
  success: false,
  errors: [
    {
      code: 1315,
      message: 'Non-printable characters are forbidden in tags'
    },
    { code: 1016, message: 'One or more cache purge errors' }
  ],
  messages: [],
  result: null
}

In this case we should delete the tags.

It could also be rate limited (HTTP Status 429)

{
  success: false,
  errors: [
    {
      code: 1134,
      message: 'Unable to purge, rate limit reached. Please wait and consider throttling your request speed'
    }
  ],
  messages: [],
  result: null
}

For this particular case, we should bail out and not delete the tags

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants