Repository navigation
{% cache %} compat follow-ups: docs, callback leak fix, regression tests - #1651
Merged
Merged
Conversation
Follow-ups to #1619 (Django {% cache %} tag compatibility): - Docs: new "Using Django's {% cache %} tag with components" section in docs/concepts/advanced/component_caching.md covering the real gotchas (same fragment embedded twice -> handlers attached more than once; JS/CSS variables silently lost after a deploy with a persistent fragment cache; vary_on hygiene), plus the process-global tag patch. - Fix: bounded callback-dict accumulation in _assemble_cached_fragment. The two synthetic callbacks added to the tree's callback dicts per {% cache %} invocation are now popped via try/finally, so a template- level loop with many {% cache %} calls doesn't leave one dead-lambda pair per iteration. - Tests: two regression tests. - test_cache_embedded_twice_on_same_page_freezes_inner_render_id pins the documented "frozen data-djc-id" behavior so a future change is forced to discuss it. - test_error_in_cache_body_does_not_poison_cache verifies that an error mid-cache-body doesn't write a partial value. - Module docstring rewrite + body annotation in cache_tag.py. The old docstring described an earlier, abandoned implementation approach; the new one matches the actual implementation. The DjcCacheNode.render body is now marked to show which lines are copied verbatim from Django's CacheNode.render (Django 5.2) and which are djc-specific, so future maintainers can re-diff against upstream when Django bumps. The architectural fix that would dissolve both documented limitations (cache RenderObject instead of the rendered string) is tracked in #1650 for v3. Co-Authored-By: Claude Opus 4.7 <[email protected]>
Contributor
Performance Benchmark ResultsComparing PR changes against master branch: Benchmarks that have stayed the same:
|
JuroOravec
commented
May 30, 2026
|
|
||
|
|
||
| class DjcCacheNode(CacheNode): | ||
| def render(self, context: Context) -> str: |
Collaborator
Author
There was a problem hiding this comment.
Added commnets here to it's clearer which parts were copied from Django and which are ours.
JuroOravec
commented
May 30, 2026
| # Pop the callbacks in `finally` so a template-level loop with many | ||
| # {% cache %} invocations doesn't leave one set of dead lambdas per | ||
| # iteration in the (still-live) tree's callback dicts. | ||
| try: |
Collaborator
Author
There was a problem hiding this comment.
One code change - component_post_render() is wrapped in try/finally, and callbacks are removed after teh code is done.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(Juro)
As I read through #1619, I explored and found:
{% cache %}tag.I captured the "proper" solution here #1650.
But since the proper design would be possibly only in v3, right now I only documented what to be aware of when using the
{% cache %}tag with components.(Opus 4.8)
Summary
Follow-ups to #1619 (Django
{% cache %}tag compatibility with components):Docs. New "Using Django's
{% cache %}tag with components" section in docs/concepts/advanced/component_caching.md. Covers the three real gotchas users can hit (same cached fragment embedded twice on a page → handlers attached more than once; JS/CSS variables silently lost after a deploy with a persistent fragment cache;vary_onhygiene), and documents the process-global patch of Django's built-in{% cache %}tag.Fix. Bounded callback-dict accumulation in
_assemble_cached_fragment: the two synthetic callbacks added totree.on_component_intermediate_callbacks/tree.on_component_rendered_callbacksper{% cache %}invocation are now popped viatry/finally, so a template-level loop with many{% cache %}calls doesn't leave one dead-lambda pair per iteration in the (still-live) tree's callback dicts.Tests. Two regression tests:
test_cache_embedded_twice_on_same_page_freezes_inner_render_id— pins the documented "frozendata-djc-id" behavior so a future change is forced to discuss it.test_error_in_cache_body_does_not_poison_cache— verifies that an error mid-cache-body doesn't write a partial value (the second render must run the component again, not return a poisoned hit).Docs/comments. Rewrote the cache_tag.py module docstring (the old text described an earlier, abandoned implementation approach), and annotated the
DjcCacheNode.renderbody to mark which lines are copied verbatim from Django'sCacheNode.render(Django 5.2) and which are djc-specific. Helps future maintainers re-diff against upstream when Django bumps.The architectural fix that would dissolve both documented limitations entirely (cache
RenderObjectinstead of the rendered string) is tracked in #1650 for v3.🤖 Generated with Claude Code