diff --git a/debug_toolbar/panels/cache.py b/debug_toolbar/panels/cache.py index bfaf7018b..e23366e37 100644 --- a/debug_toolbar/panels/cache.py +++ b/debug_toolbar/panels/cache.py @@ -5,7 +5,12 @@ from django.conf import settings from django.core import cache -from django.core.cache import CacheHandler, caches as original_caches +from django.core.cache import ( + DEFAULT_CACHE_ALIAS, + CacheHandler, + cache as original_cache, + caches as original_caches, +) from django.core.cache.backends.base import BaseCache from django.dispatch import Signal from django.middleware import cache as middleware_cache @@ -133,7 +138,14 @@ def decr_version(self, *args, **kwargs): class CacheHandlerPatch(CacheHandler): def __getitem__(self, alias): actual_cache = super().__getitem__(alias) - return CacheStatTracker(actual_cache) + return ( + actual_cache + if isinstance(actual_cache, CacheStatTracker) + else CacheStatTracker(actual_cache) + ) + + def create_connection(self, alias): + return CacheStatTracker(super().create_connection(alias)) middleware_cache.caches = CacheHandlerPatch() @@ -241,17 +253,18 @@ def title(self): ) def enable_instrumentation(self): - if isinstance(middleware_cache.caches, CacheHandlerPatch): - cache.caches = middleware_cache.caches - else: - cache.caches = CacheHandlerPatch() + if not isinstance(middleware_cache.caches, CacheHandlerPatch): + middleware_cache.caches = CacheHandlerPatch() + cache.caches = middleware_cache.caches + cache.cache = cache.caches[DEFAULT_CACHE_ALIAS] def disable_instrumentation(self): - cache.caches = original_caches # While it can be restored to the original, any views that were # wrapped with the cache_page decorator will continue to use a # monkey patched cache. middleware_cache.caches = original_caches + cache.caches = original_caches + cache.cache = original_cache def generate_stats(self, request, response): self.record_stats(