Skip to content

[6.x] Batch URL invalidation in the application static cacher - #15778

Merged
jasonvarga merged 2 commits into
statamic:6.xfrom
ryanmitchell:batch-application-cacher-invalidation
Nov 9, 2026
Merged

jasonvarga merged 2 commits into
statamic:6.xfrom
ryanmitchell:batch-application-cacher-invalidation

Conversation

@ryanmitchell

Copy link
Copy Markdown
Contributor

ApplicationCacher::invalidateUrls() invalidated each URL individually, and every invalidateUrl() re-reads the domain's whole URL index and rewrites it once per matching key. Invalidating many URLs at once (e.g. a product that appears on lots of listing pages) on a site with a large index meant many full reads and writes of a large cache key.

invalidateUrls() now groups the URLs by domain, expands wildcards into the same batch, and rewrites each domain's index once via a new locked AbstractCacher::forgetUrls(). Matching (exact path or any query string version of it) and UrlInvalidated events are unchanged.

Worth saying: invalidateUrls() no longer calls invalidateUrl() once per URL. A subclass that overrides only invalidateUrl() won't see batched calls any more.

Benchmarks

Cached pages Before this PR After this PR Speed-up
20,000 448ms and 482ms per row 122ms and 122ms per row ~3.8×
50,000 2,814ms and 3,036ms per row 231ms and 263ms per row ~12×

ApplicationCacher::invalidateUrls() invalidated each URL individually, and
every invalidateUrl() re-reads the domain's whole URL index and rewrites it
once per matching key. Invalidating many URLs at once (e.g. a product
that appears on lots of listing pages) on a site with a large index meant
many full reads and writes of a large cache key.

invalidateUrls() now groups the URLs by domain, expands wildcards into the
same batch, and rewrites each domain's index once via a new locked
AbstractCacher::forgetUrls(). Matching (exact path or any query string
version of it) and UrlInvalidated events are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@duncanmcclean duncanmcclean left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this, the speed-up is great and the batched-vs-single test is a nice touch.

One thing before we merge: the number of UrlInvalidated events dispatched has changed, and people listen to that event (e.g. to purge a CDN), so I'd consider it a breaking change.

  • A URL passed twice, or passed and also matched by a wildcard, now dispatches one event instead of two (because of ->unique()).
  • Wildcards are expanded before anything is removed. So for ['/a', '/a*'] with /a?x cached, there's now an extra event for /a?x, which wasn't dispatched before because invalidating /a had already removed it.
  • Events are dispatched after all removals, grouped by domain, rather than in input order.

Could we keep the single index write, but work out the events the same way the old loop did? Something like:

  • Walk the URLs in input order against an in-memory copy of each domain's index.
  • For a normal URL, collect its matching keys and dispatch once for it (even if nothing matched, or it's a duplicate).
  • For a wildcard, match against that in-memory index minus keys already removed in this run, and dispatch once per match.
  • After the loop, forget the responses and call forgetUrls() once per domain.

It'd be good to extend invalidating_multiple_urls_matches_invalidating_them_one_at_a_time to assert the dispatched events (and their order) match too.

Rather than grouping and de-duplicating, walk the URLs in input order
against an in-memory copy of each domain's index, the same way the
one-at-a-time loop did, so the same events are dispatched in the same
order (duplicates, URLs also matched by a wildcard, and wildcard matches
already removed by an earlier URL included). Each domain's index is still
rewritten once, and events are dispatched after everything is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ryanmitchell

Copy link
Copy Markdown
Contributor Author

Sure, thats done and pushed. One difference from what you suggested: events are collected during the walk and dispatched after all the removals, in the original order. If they fired mid-walk, a listener like a CDN purge could trigger a re-fetch while the page was still in the static cache, and the stale page would get cached again. The old loop always removed a URL before dispatching its event, so this keeps that guarantee. The only change is that while an event fires, URLs later in the list have already been removed too.

@jasonvarga
jasonvarga merged commit 759f2df into statamic:6.x Nov 9, 2026
61 checks passed
@ryanmitchell
ryanmitchell deleted the batch-application-cacher-invalidation branch November 9, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants