Repository navigation
[6.x] Batch URL invalidation in the application static cacher - #15778
Conversation
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
left a comment
There was a problem hiding this comment.
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?xcached, there's now an extra event for/a?x, which wasn't dispatched before because invalidating/ahad 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>
|
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. |
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