|
6 | 6 |
|
7 | 7 | from django.conf import settings
|
8 | 8 | 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) |
10 | 12 | from django.core.cache.backends.base import BaseCache
|
11 | 13 | from django.dispatch import Signal
|
12 | 14 | 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 |
13 | 21 | try:
|
14 | 22 | from collections import OrderedDict
|
15 | 23 | except ImportError:
|
@@ -119,6 +127,17 @@ def get_cache(*args, **kwargs):
|
119 | 127 | return CacheStatTracker(original_get_cache(*args, **kwargs))
|
120 | 128 |
|
121 | 129 |
|
| 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 | + |
122 | 141 | class CachePanel(Panel):
|
123 | 142 | """
|
124 | 143 | Panel that displays the cache statistics.
|
@@ -197,11 +216,17 @@ def title(self):
|
197 | 216 | def enable_instrumentation(self):
|
198 | 217 | # This isn't thread-safe because cache connections aren't thread-local
|
199 | 218 | # in Django, unlike database connections.
|
200 |
| - cache.cache = CacheStatTracker(original_cache) |
201 | 219 | cache.get_cache = get_cache
|
| 220 | + if CacheHandler is None: |
| 221 | + cache.cache = CacheStatTracker(original_cache) |
| 222 | + else: |
| 223 | + cache.caches = get_cache_handler() |
202 | 224 |
|
203 | 225 | 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 |
205 | 230 | cache.get_cache = original_get_cache
|
206 | 231 |
|
207 | 232 | def process_response(self, request, response):
|
|
0 commit comments