Skip to content

Commit 02e8ea1

Browse files
committed
Support the caches logic introduced in Django 1.7.
1 parent e18af06 commit 02e8ea1

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

debug_toolbar/panels/cache.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66

77
from django.conf import settings
88
from django.core import cache
9-
from django.core.cache import cache as original_cache, get_cache as original_get_cache
9+
from django.core.cache import (
10+
cache as original_cache,
11+
get_cache as original_get_cache)
1012
from django.core.cache.backends.base import BaseCache
1113
from django.dispatch import Signal
1214
from django.utils.translation import ugettext_lazy as _, ungettext
15+
16+
try:
17+
from django.core.cache import CacheHandler, caches as original_caches
18+
except ImportError: # Django < 1.7
19+
CacheHandler = None
20+
original_caches = None
1321
try:
1422
from collections import OrderedDict
1523
except ImportError:
@@ -119,6 +127,17 @@ def get_cache(*args, **kwargs):
119127
return CacheStatTracker(original_get_cache(*args, **kwargs))
120128

121129

130+
def get_cache_handler():
131+
if CacheHandler is None:
132+
return None
133+
134+
class CacheHandlerPatch(CacheHandler):
135+
def __getitem__(self, alias):
136+
actual_cache = super(CacheHandlerPatch, self).__getitem__(alias)
137+
return CacheStatTracker(actual_cache)
138+
return CacheHandlerPatch()
139+
140+
122141
class CachePanel(Panel):
123142
"""
124143
Panel that displays the cache statistics.
@@ -197,11 +216,17 @@ def title(self):
197216
def enable_instrumentation(self):
198217
# This isn't thread-safe because cache connections aren't thread-local
199218
# in Django, unlike database connections.
200-
cache.cache = CacheStatTracker(original_cache)
201219
cache.get_cache = get_cache
220+
if CacheHandler is None:
221+
cache.cache = CacheStatTracker(original_cache)
222+
else:
223+
cache.caches = get_cache_handler()
202224

203225
def disable_instrumentation(self):
204-
cache.cache = original_cache
226+
if CacheHandler is None:
227+
cache.cache = original_cache
228+
else:
229+
cache.caches = original_caches
205230
cache.get_cache = original_get_cache
206231

207232
def process_response(self, request, response):

tests/panels/test_cache.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
from __future__ import absolute_import, unicode_literals
44

5+
import django
56
from django.core import cache
7+
from django.utils.unittest import skipIf
68

79
from ..base import BaseTestCase
810

@@ -24,3 +26,10 @@ def test_recording(self):
2426
cache.cache.get('foo')
2527
cache.cache.delete('foo')
2628
self.assertEqual(len(self.panel.calls), 3)
29+
30+
@skipIf(django.VERSION < (1, 7), "Caches was added in Django 1.7")
31+
def test_recording_caches(self):
32+
self.assertEqual(len(self.panel.calls), 0)
33+
cache.cache.set('foo', 'bar')
34+
cache.caches[cache.DEFAULT_CACHE_ALIAS].get('foo')
35+
self.assertEqual(len(self.panel.calls), 2)

0 commit comments

Comments
 (0)