Skip to content

Commit 9c2d243

Browse files
Show cache alias and class name instead of backend repr in cache panel (#2219)
* Show cache alias and backend class name instead of backend repr * Use the _djdt_panel as the patched indicator. --------- Co-authored-by: Tim Schilling <schillingt@better-simple.com>
1 parent c9ce618 commit 9c2d243

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

debug_toolbar/panels/cache.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
]
3131

3232

33-
def _monkey_patch_method(cache, name):
33+
def _monkey_patch_method(cache, name, alias):
3434
original_method = getattr(cache, name)
3535

3636
@functools.wraps(original_method)
@@ -39,16 +39,16 @@ def wrapper(*args, **kwargs):
3939
if panel is None:
4040
return original_method(*args, **kwargs)
4141
else:
42-
return panel._record_call(cache, name, original_method, args, kwargs)
42+
return panel._record_call(cache, alias, name, original_method, args, kwargs)
4343

4444
setattr(cache, name, wrapper)
4545

4646

47-
def _monkey_patch_cache(cache):
48-
if not hasattr(cache, "_djdt_patched"):
47+
def _monkey_patch_cache(cache, alias, panel):
48+
if not getattr(cache, "_djdt_panel", None):
4949
for name in WRAPPED_CACHE_METHODS:
50-
_monkey_patch_method(cache, name)
51-
cache._djdt_patched = True
50+
_monkey_patch_method(cache, name, alias)
51+
cache._djdt_panel = panel
5252

5353

5454
class CachePanel(Panel):
@@ -95,8 +95,7 @@ def wrapper(self, alias):
9595
cache = original_method(self, alias)
9696
panel = cls.current_instance()
9797
if panel is not None:
98-
_monkey_patch_cache(cache)
99-
cache._djdt_panel = panel
98+
_monkey_patch_cache(cache, alias, panel)
10099
return cache
101100

102101
CacheHandler.create_connection = wrapper
@@ -138,7 +137,7 @@ def _store_call_info(
138137
}
139138
)
140139

141-
def _record_call(self, cache, name, original_method, args, kwargs):
140+
def _record_call(self, cache, alias, name, original_method, args, kwargs):
142141
# Some cache backends implement certain cache methods in terms of other cache
143142
# methods (e.g. get_or_set() in terms of get() and add()). In order to only
144143
# record the calls made directly by the user code, set the cache's _djdt_panel
@@ -161,7 +160,7 @@ def _record_call(self, cache, name, original_method, args, kwargs):
161160
kwargs=kwargs,
162161
trace=get_stack_trace(skip=2),
163162
template_info=get_template_info(),
164-
backend=cache,
163+
backend=f"{alias} ({type(cache).__name__})",
165164
)
166165
return value
167166

@@ -194,9 +193,8 @@ def enable_instrumentation(self):
194193
# requests. The monkey patch of CacheHander.create_connection() installed in
195194
# the .ready() method will ensure that any new cache connections that get opened
196195
# during this request will also be monkey patched.
197-
for cache in caches.all(initialized_only=True):
198-
_monkey_patch_cache(cache)
199-
cache._djdt_panel = self
196+
for cache, alias in self.initialized_caches():
197+
_monkey_patch_cache(cache, alias, self)
200198
# Mark this panel instance as the current one for the active thread/async task
201199
# context. This will be used by the CacheHander.create_connection() monkey
202200
# patch.
@@ -208,6 +206,17 @@ def disable_instrumentation(self):
208206
for cache in caches.all(initialized_only=True):
209207
cache._djdt_panel = None
210208

209+
def initialized_caches(self):
210+
"""
211+
Return the initialized caches and aliases.
212+
213+
This does the same as`caches.all(initialized_only=True)`, but keeps
214+
the alias with each cache instance.
215+
"""
216+
for alias in caches:
217+
if hasattr(caches._connections, alias):
218+
yield caches[alias], alias
219+
211220
def generate_stats(self, request, response):
212221
self.record_stats(
213222
{

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Pending
88
toolbar data from redirected requests.
99
* Fixed support for generating code coverage comments in PRs.
1010
* Added Django 6.0 to the testing matrix.
11+
* Show the cache backend alias and cache backend class name instead of
12+
the cache instance in the cache panel.
1113

1214
6.1.0 (2025-10-30)
1315
------------------

tests/panels/test_cache.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,7 @@ def test_generate_server_timing(self):
154154
}
155155

156156
self.assertEqual(self.panel.get_server_timing_stats(), expected_data)
157+
158+
def test_backend_alias_is_recorded(self):
159+
cache.cache.get("foo")
160+
self.assertEqual(self.panel.calls[0]["backend"], "default (LocMemCache)")

0 commit comments

Comments
 (0)