Skip to content

Really ugly work in progress hack to fix tests on Django@master #1427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions debug_toolbar/panels/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down