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
5454class 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 {
0 commit comments