Skip to content

Surgical Django cache purge - #609

Open
skaphan wants to merge 3 commits into
artoonie:mainfrom
skaphan:surgical-cache-purge
Open

Surgical Django cache purge#609
skaphan wants to merge 3 commits into
artoonie:mainfrom
skaphan:surgical-cache-purge

Conversation

@skaphan

@skaphan skaphan commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replaces cache.clear() with surgical purge that only deletes cache entries for the affected slug's URLs
  • Uses thread-local middleware to capture request host for accurate cache key reconstruction
  • Catches DisallowedHost instead of temporarily mutating settings.ALLOWED_HOSTS (addresses review concern from Fix server-side cache to work with conditional GET #608)
  • Comprehensive test verifies all URL variants (including query-string vistypes) are purged

Follow-up optimization to #608. This is a nice-to-have, not critical — #608's cache.clear() is correct and safe.

Test plan

  • test_purge_vis_cache_clears_all_cached_urls — populates cache for base, embedded, and query-string URLs, purges by slug, verifies all gone
  • test_purge_django_cache_tries_www_variant — verifies www. domain variant is included in purge domains

🤖 Generated with Claude Code

with open(filenames.ONE_ROUND, 'r', encoding='utf-8') as f:
self.client.post('/upload.html', {'jsonFile': f})
config = TestHelpers.get_latest_upload()
path = reverse('visualize', args=(config.slug,))

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable path is not used.

Copilot Autofix

AI 10 days ago

The safest fix is to remove the unused assignment while preserving behavior.

  • General approach: if a variable is assigned and never used, delete the assignment unless the right-hand side has required side effects.
  • Here: reverse('visualize', args=(config.slug,)) is side-effect free in this context, and the resulting path is unused, so remove that line.
  • File/region: visualizer/tests/testSimple.py, inside test_purge_django_cache_tries_www_variant, remove line with path = ....
  • No imports/methods/definitions are needed.
Suggested changeset 1
visualizer/tests/testSimple.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/visualizer/tests/testSimple.py b/visualizer/tests/testSimple.py
--- a/visualizer/tests/testSimple.py
+++ b/visualizer/tests/testSimple.py
@@ -731,7 +731,6 @@
         with open(filenames.ONE_ROUND, 'r', encoding='utf-8') as f:
             self.client.post('/upload.html', {'jsonFile': f})
         config = TestHelpers.get_latest_upload()
-        path = reverse('visualize', args=(config.slug,))
 
         # _get_purge_domains should include both example.com and www.example.com
         domains = CloudflareAPI._get_purge_domains()
EOF
@@ -731,7 +731,6 @@
with open(filenames.ONE_ROUND, 'r', encoding='utf-8') as f:
self.client.post('/upload.html', {'jsonFile': f})
config = TestHelpers.get_latest_upload()
path = reverse('visualize', args=(config.slug,))

# _get_purge_domains should include both example.com and www.example.com
domains = CloudflareAPI._get_purge_domains()
Copilot is powered by AI and may make mistakes. Always verify output.
@skaphan
skaphan force-pushed the surgical-cache-purge branch from 7029d2a to 9916f83 Compare March 20, 2026 19:23
@skaphan
skaphan force-pushed the surgical-cache-purge branch from 9916f83 to 993bb17 Compare April 8, 2026 02:05
skaphan added 3 commits April 10, 2026 09:55
Uses thread-local middleware to capture the current request host,
then builds synthetic requests matching each cached URL to delete
only the affected cache entries instead of clearing everything.

Co-Authored-By: Claude Opus 4.6
Replace temporary ALLOWED_HOSTS mutation with DisallowedHost
catch-and-skip to avoid modifying security settings at runtime.
Replace two separate purge tests with one comprehensive test that
verifies purge_vis_cache clears all cached URL variants for a slug,
including query-string variants.

Co-Authored-By: Claude Opus 4.6
@skaphan
skaphan force-pushed the surgical-cache-purge branch from 993bb17 to 1675b64 Compare April 10, 2026 16:56
@artoonie artoonie mentioned this pull request Aug 10, 2026
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.

2 participants