@@ -44,11 +44,12 @@ def wrapper(*args, **kwargs):
4444 setattr (cache , name , wrapper )
4545
4646
47- def _monkey_patch_cache (cache , alias ):
47+ def _monkey_patch_cache (cache , alias , panel ):
4848 if not hasattr (cache , "_djdt_patched" ):
4949 for name in WRAPPED_CACHE_METHODS :
5050 _monkey_patch_method (cache , name , alias )
5151 cache ._djdt_patched = True
52+ cache ._djdt_panel = panel
5253
5354
5455class CachePanel (Panel ):
@@ -95,8 +96,7 @@ def wrapper(self, alias):
9596 cache = original_method (self , alias )
9697 panel = cls .current_instance ()
9798 if panel is not None :
98- _monkey_patch_cache (cache , alias )
99- cache ._djdt_panel = panel
99+ _monkey_patch_cache (cache , alias , panel )
100100 return cache
101101
102102 CacheHandler .create_connection = wrapper
@@ -194,10 +194,8 @@ 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 alias in caches :
198- if hasattr (caches ._connections , alias ):
199- _monkey_patch_cache (caches [alias ], alias )
200- caches [alias ]._djdt_panel = self
197+ for cache , alias in self .initialized_caches ():
198+ _monkey_patch_cache (cache , alias , self )
201199 # Mark this panel instance as the current one for the active thread/async task
202200 # context. This will be used by the CacheHander.create_connection() monkey
203201 # patch.
@@ -209,6 +207,17 @@ def disable_instrumentation(self):
209207 for cache in caches .all (initialized_only = True ):
210208 cache ._djdt_panel = None
211209
210+ def initialized_caches (self ):
211+ """
212+ Return the initialized caches and aliases.
213+
214+ This does the same as`caches.all(initialized_only=True)`, but keeps
215+ the alias with each cache instance.
216+ """
217+ for alias in caches :
218+ if hasattr (caches ._connections , alias ):
219+ yield caches [alias ], alias
220+
212221 def generate_stats (self , request , response ):
213222 self .record_stats (
214223 {
0 commit comments