|
8 | 8 | except ImportError:
|
9 | 9 | ConnectionProxy = None
|
10 | 10 |
|
| 11 | +import django |
11 | 12 | from django.conf import settings
|
12 | 13 | from django.core import cache
|
13 | 14 | from django.core.cache import (
|
@@ -140,10 +141,27 @@ def decr_version(self, *args, **kwargs):
|
140 | 141 | return self.cache.decr_version(*args, **kwargs)
|
141 | 142 |
|
142 | 143 |
|
143 |
| -class CacheHandlerPatch(CacheHandler): |
144 |
| - def __getitem__(self, alias): |
145 |
| - actual_cache = super().__getitem__(alias) |
146 |
| - return CacheStatTracker(actual_cache) |
| 144 | +if django.VERSION < (3, 2): |
| 145 | + |
| 146 | + class CacheHandlerPatch(CacheHandler): |
| 147 | + def __getitem__(self, alias): |
| 148 | + actual_cache = super().__getitem__(alias) |
| 149 | + return CacheStatTracker(actual_cache) |
| 150 | + |
| 151 | + |
| 152 | +else: |
| 153 | + |
| 154 | + class CacheHandlerPatch(CacheHandler): |
| 155 | + def __init__(self, settings=None): |
| 156 | + self._djdt_wrap = True |
| 157 | + super().__init__(settings=settings) |
| 158 | + |
| 159 | + def create_connection(self, alias): |
| 160 | + actual_cache = super().create_connection(alias) |
| 161 | + if self._djdt_wrap: |
| 162 | + return CacheStatTracker(actual_cache) |
| 163 | + else: |
| 164 | + return actual_cache |
147 | 165 |
|
148 | 166 |
|
149 | 167 | middleware_cache.caches = CacheHandlerPatch()
|
@@ -251,22 +269,40 @@ def title(self):
|
251 | 269 | )
|
252 | 270 |
|
253 | 271 | def enable_instrumentation(self):
|
254 |
| - if isinstance(middleware_cache.caches, CacheHandlerPatch): |
255 |
| - cache.caches = middleware_cache.caches |
| 272 | + if django.VERSION < (3, 2): |
| 273 | + if isinstance(middleware_cache.caches, CacheHandlerPatch): |
| 274 | + cache.caches = middleware_cache.caches |
| 275 | + else: |
| 276 | + cache.caches = CacheHandlerPatch() |
256 | 277 | else:
|
257 |
| - cache.caches = CacheHandlerPatch() |
| 278 | + for alias in cache.caches: |
| 279 | + if not isinstance(cache.caches[alias], CacheStatTracker): |
| 280 | + cache.caches[alias] = CacheStatTracker(cache.caches[alias]) |
| 281 | + |
| 282 | + if not isinstance(middleware_cache.caches, CacheHandlerPatch): |
| 283 | + middleware_cache.caches = cache.caches |
258 | 284 |
|
259 | 285 | # Wrap the patched cache inside Django's ConnectionProxy
|
260 | 286 | if ConnectionProxy:
|
261 | 287 | cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS)
|
262 | 288 |
|
263 | 289 | def disable_instrumentation(self):
|
264 |
| - cache.caches = original_caches |
265 |
| - cache.cache = original_cache |
266 |
| - # While it can be restored to the original, any views that were |
267 |
| - # wrapped with the cache_page decorator will continue to use a |
268 |
| - # monkey patched cache. |
269 |
| - middleware_cache.caches = original_caches |
| 290 | + if django.VERSION < (3, 2): |
| 291 | + cache.caches = original_caches |
| 292 | + cache.cache = original_cache |
| 293 | + # While it can be restored to the original, any views that were |
| 294 | + # wrapped with the cache_page decorator will continue to use a |
| 295 | + # monkey patched cache. |
| 296 | + middleware_cache.caches = original_caches |
| 297 | + else: |
| 298 | + for alias in cache.caches: |
| 299 | + if isinstance(cache.caches[alias], CacheStatTracker): |
| 300 | + cache.caches[alias] = cache.caches[alias].cache |
| 301 | + if ConnectionProxy: |
| 302 | + cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS) |
| 303 | + # While it can be restored to the original, any views that were |
| 304 | + # wrapped with the cache_page decorator will continue to use a |
| 305 | + # monkey patched cache. |
270 | 306 |
|
271 | 307 | def generate_stats(self, request, response):
|
272 | 308 | self.record_stats(
|
|
0 commit comments