Skip to content

Surgical cache purge registry - #644

Draft
artoonie wants to merge 2 commits into
mainfrom
surgical-cache-purge-registry
Draft

artoonie wants to merge 2 commits into
mainfrom
surgical-cache-purge-registry

Conversation

@artoonie

Copy link
Copy Markdown
Owner

No description provided.

artoonie and others added 2 commits September 11, 2026 23:38
Saving a JsonConfig used to cache.clear(), evicting every cached page so
that every visualization recomputed its graph on its next origin miss.
Now a save deletes exactly the pages of that slug.

Page cache keys depend on the scheme, host, path, query string and Vary
headers of the request that stored them, and this code runs at several
live URLs, so reconstructing keys from a slug is guesswork. Instead the
real keys are recorded when Django stores the page: the view mixin tags
the request with the slug, and PageCacheRegistryMiddleware, listed above
UpdateCacheMiddleware so it runs after the store, asks get_cache_key for
the key a later viewer will hit and appends it to a per-slug registry in
the same cache. purge_vis_cache deletes the registered keys.

Every doubtful case falls back to clearing everything, so a purge can be
wasteful but never leaves a stale page: no registry for the slug, a
registry that overflowed, or a registry lock that could not be taken.
Registry updates are serialized across gunicorn workers with a lock
file in the cache directory, since the misses for a slug's URLs arrive
together right after a purge and a lost update would strand a page.

Cache hits never run the view, so they carry no tag and the middleware
returns immediately: the zero-database hit path is unchanged.

Election pages keep the clear-everything purge; their views are untagged.

Tests view pages through two different hosts and schemes and assert
the save evicts all of them while another visualization stays cached
and is still served with zero queries. The headless cache tests now
also assert that updating another visualization leaves this one cached.
Both test classes start from an empty page cache, since the file cache
outlives tests. The purge tests live in their own module because
testSimple.py hit pylint's 1000-line limit.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013JDT5wxbhPUtZG1kXPZ1Xe
Django's default evicts a random third of the cache on every write once
300 files exist. Each cached page costs two files, so the default held
about 150 pages, fewer than the URL variants of a dozen busy
visualizations, and each eviction is a graph recomputation at the
origin. A page cache registry evicted this way also forces the next
purge to clear everything.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013JDT5wxbhPUtZG1kXPZ1Xe
@artoonie artoonie mentioned this pull request Sep 12, 2026
2 tasks
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.

1 participant