Skip to content

Commit 88d911d

Browse files
committed
Show cache alias instead of backend repr
1 parent 5002cf8 commit 88d911d

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

debug_toolbar/panels/cache.py

Lines changed: 11 additions & 10 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,15 +39,15 @@ 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):
47+
def _monkey_patch_cache(cache, alias):
4848
if not hasattr(cache, "_djdt_patched"):
4949
for name in WRAPPED_CACHE_METHODS:
50-
_monkey_patch_method(cache, name)
50+
_monkey_patch_method(cache, name, alias)
5151
cache._djdt_patched = True
5252

5353

@@ -95,7 +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)
98+
_monkey_patch_cache(cache, alias)
9999
cache._djdt_panel = panel
100100
return cache
101101

@@ -138,7 +138,7 @@ def _store_call_info(
138138
}
139139
)
140140

141-
def _record_call(self, cache, name, original_method, args, kwargs):
141+
def _record_call(self, cache, alias, name, original_method, args, kwargs):
142142
# Some cache backends implement certain cache methods in terms of other cache
143143
# methods (e.g. get_or_set() in terms of get() and add()). In order to only
144144
# record the calls made directly by the user code, set the cache's _djdt_panel
@@ -161,7 +161,7 @@ def _record_call(self, cache, name, original_method, args, kwargs):
161161
kwargs=kwargs,
162162
trace=get_stack_trace(skip=2),
163163
template_info=get_template_info(),
164-
backend=cache,
164+
backend=alias,
165165
)
166166
return value
167167

@@ -194,9 +194,10 @@ def enable_instrumentation(self):
194194
# requests. The monkey patch of CacheHander.create_connection() installed in
195195
# the .ready() method will ensure that any new cache connections that get opened
196196
# 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
197+
for alias in caches:
198+
if hasattr(caches._connections, alias):
199+
_monkey_patch_cache(caches[alias], alias)
200+
caches[alias]._djdt_panel = self
200201
# Mark this panel instance as the current one for the active thread/async task
201202
# context. This will be used by the CacheHander.create_connection() monkey
202203
# patch.

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Pending
6363
conflicts
6464
* Added CSS for resetting the height of elements too to avoid problems with
6565
global CSS of a website where the toolbar is used.
66+
* Show the cache backend alias instead of the cache instance for each call in
67+
the cache panel.
6668

6769
5.1.0 (2025-03-20)
6870
------------------

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")

0 commit comments

Comments
 (0)