Skip to content

Commit 7403bed

Browse files
Only patch the cache methods once.
1 parent 0d25b3a commit 7403bed

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

debug_toolbar/panels/cache.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ def wrapper(*args, **kwargs):
4545

4646

4747
def _monkey_patch_cache(cache, alias, panel):
48-
if not getattr(cache, "_djdt_panel", None):
48+
if not hasattr(cache, "_djdt_panel"):
49+
# The panel has never been monkey patched before
4950
for name in WRAPPED_CACHE_METHODS:
5051
_monkey_patch_method(cache, name, alias)
52+
if not getattr(cache, "_djdt_panel", None):
53+
# This is used for both initially monkey patching and re-enabling the
54+
# instrumentation.
5155
cache._djdt_panel = panel
5256

5357

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Pending
1616
rather than on instantiation.
1717
* Highlighted the documentation about disabling the browser's caching to
1818
ensure the latest static assets are used.
19+
* Fixed bug with ``CachePanel`` so the cache patching is only applied
20+
once.
1921

2022
6.2.0 (2026-01-20)
2123
------------------

tests/panels/test_cache.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,21 @@ def test_generate_server_timing(self):
158158
def test_backend_alias_is_recorded(self):
159159
cache.cache.get("foo")
160160
self.assertEqual(self.panel.calls[0]["backend"], "default (LocMemCache)")
161+
162+
def test_patching_only_applied_once(self):
163+
"""
164+
Confirm that reapplying and disabling the cache patching only wraps
165+
the cache methods once
166+
"""
167+
for _ in range(2):
168+
self.panel.enable_instrumentation()
169+
self.panel.disable_instrumentation()
170+
nested_wraps = 0
171+
wrapped = cache.cache.get.__wrapped__
172+
while wrapped is not None:
173+
try:
174+
wrapped = wrapped.__wrapped__
175+
nested_wraps += 1
176+
except AttributeError:
177+
break
178+
self.assertEqual(nested_wraps, 0)

0 commit comments

Comments
 (0)