66
77from django .conf import settings
88from django .core import cache
9- from django .core .cache import get_cache as base_get_cache
9+ from django .core .cache import cache as original_cache , get_cache as original_get_cache
1010from django .core .cache .backends .base import BaseCache
1111from django .dispatch import Signal
1212from django .template import Node
@@ -123,6 +123,10 @@ def decr_version(self, *args, **kwargs):
123123 return self .cache .decr_version (* args , ** kwargs )
124124
125125
126+ def get_cache (* args , ** kwargs ):
127+ return CacheStatTracker (original_get_cache (* args , ** kwargs ))
128+
129+
126130class CacheDebugPanel (DebugPanel ):
127131 """
128132 Panel that displays the cache statistics.
@@ -195,6 +199,16 @@ def title(self):
195199 'Cache calls from %(count)d backends' ,
196200 count ) % dict (count = count )
197201
202+ def enable_instrumentation (self ):
203+ # This isn't thread-safe because cache connections aren't thread-local
204+ # in Django, unlike database connections.
205+ cache .cache = CacheStatTracker (cache .cache )
206+ cache .get_cache = get_cache
207+
208+ def disable_instrumentation (self ):
209+ cache .cache = original_cache
210+ cache .get_cache = original_get_cache
211+
198212 def process_response (self , request , response ):
199213 self .record_stats ({
200214 'total_calls' : len (self .calls ),
@@ -204,12 +218,3 @@ def process_response(self, request, response):
204218 'misses' : self .misses ,
205219 'counts' : self .counts ,
206220 })
207-
208-
209- def get_cache_debug (* args , ** kwargs ):
210- base_cache = base_get_cache (* args , ** kwargs )
211- return CacheStatTracker (base_cache )
212-
213-
214- cache .cache = CacheStatTracker (cache .cache )
215- cache .get_cache = get_cache_debug
0 commit comments