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