diff --git a/debug_toolbar/panels/cache.py b/debug_toolbar/panels/cache.py index 1731fa4b8..bc82229a7 100644 --- a/debug_toolbar/panels/cache.py +++ b/debug_toolbar/panels/cache.py @@ -6,10 +6,18 @@ from django.conf import settings from django.core import cache -from django.core.cache import cache as original_cache, get_cache as original_get_cache +from django.core.cache import ( + cache as original_cache, + get_cache as original_get_cache) from django.core.cache.backends.base import BaseCache from django.dispatch import Signal from django.utils.translation import ugettext_lazy as _, ungettext + +try: + from django.core.cache import CacheHandler, caches as original_caches +except ImportError: # Django < 1.7 + CacheHandler = None + original_caches = None try: from collections import OrderedDict except ImportError: @@ -119,6 +127,17 @@ def get_cache(*args, **kwargs): return CacheStatTracker(original_get_cache(*args, **kwargs)) +def get_cache_handler(): + if CacheHandler is None: + return None + + class CacheHandlerPatch(CacheHandler): + def __getitem__(self, alias): + actual_cache = super(CacheHandlerPatch, self).__getitem__(alias) + return CacheStatTracker(actual_cache) + return CacheHandlerPatch() + + class CachePanel(Panel): """ Panel that displays the cache statistics. @@ -197,11 +216,17 @@ def title(self): def enable_instrumentation(self): # This isn't thread-safe because cache connections aren't thread-local # in Django, unlike database connections. - cache.cache = CacheStatTracker(original_cache) cache.get_cache = get_cache + if CacheHandler is None: + cache.cache = CacheStatTracker(original_cache) + else: + cache.caches = get_cache_handler() def disable_instrumentation(self): - cache.cache = original_cache + if CacheHandler is None: + cache.cache = original_cache + else: + cache.caches = original_caches cache.get_cache = original_get_cache def process_response(self, request, response): diff --git a/tests/panels/test_cache.py b/tests/panels/test_cache.py index 73b7e7c99..6391608b4 100644 --- a/tests/panels/test_cache.py +++ b/tests/panels/test_cache.py @@ -2,7 +2,9 @@ from __future__ import absolute_import, unicode_literals +import django from django.core import cache +from django.utils.unittest import skipIf from ..base import BaseTestCase @@ -24,3 +26,10 @@ def test_recording(self): cache.cache.get('foo') cache.cache.delete('foo') self.assertEqual(len(self.panel.calls), 3) + + @skipIf(django.VERSION < (1, 7), "Caches was added in Django 1.7") + def test_recording_caches(self): + self.assertEqual(len(self.panel.calls), 0) + cache.cache.set('foo', 'bar') + cache.caches[cache.DEFAULT_CACHE_ALIAS].get('foo') + self.assertEqual(len(self.panel.calls), 2)