|
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 | | -from django.core.cache import ( |
15 | | - DEFAULT_CACHE_ALIAS, |
16 | | - CacheHandler, |
17 | | - cache as original_cache, |
18 | | - caches as original_caches, |
19 | | -) |
| 13 | +from django.core.cache import DEFAULT_CACHE_ALIAS, CacheHandler |
20 | 14 | from django.core.cache.backends.base import BaseCache |
21 | 15 | from django.dispatch import Signal |
22 | 16 | from django.middleware import cache as middleware_cache |
@@ -69,7 +63,7 @@ def __init__(self, cache): |
69 | 63 | self.cache = cache |
70 | 64 |
|
71 | 65 | def __repr__(self): |
72 | | - return str("<CacheStatTracker for %s>") % repr(self.cache) |
| 66 | + return "<CacheStatTracker for %s>" % repr(self.cache) |
73 | 67 |
|
74 | 68 | def _get_func_info(self): |
75 | 69 | frame = sys._getframe(3) |
@@ -141,26 +135,17 @@ def decr_version(self, *args, **kwargs): |
141 | 135 | return self.cache.decr_version(*args, **kwargs) |
142 | 136 |
|
143 | 137 |
|
144 | | -if django.VERSION < (3, 2): |
| 138 | +class CacheHandlerPatch(CacheHandler): |
| 139 | + def __init__(self, settings=None): |
| 140 | + self._djdt_wrap = True |
| 141 | + super().__init__(settings=settings) |
145 | 142 |
|
146 | | - class CacheHandlerPatch(CacheHandler): |
147 | | - def __getitem__(self, alias): |
148 | | - actual_cache = super().__getitem__(alias) |
| 143 | + def create_connection(self, alias): |
| 144 | + actual_cache = super().create_connection(alias) |
| 145 | + if self._djdt_wrap: |
149 | 146 | 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 |
| 147 | + else: |
| 148 | + return actual_cache |
164 | 149 |
|
165 | 150 |
|
166 | 151 | middleware_cache.caches = CacheHandlerPatch() |
@@ -268,40 +253,26 @@ def title(self): |
268 | 253 | ) |
269 | 254 |
|
270 | 255 | 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]) |
| 256 | + for alias in cache.caches: |
| 257 | + if not isinstance(cache.caches[alias], CacheStatTracker): |
| 258 | + cache.caches[alias] = CacheStatTracker(cache.caches[alias]) |
280 | 259 |
|
281 | | - if not isinstance(middleware_cache.caches, CacheHandlerPatch): |
282 | | - middleware_cache.caches = cache.caches |
| 260 | + if not isinstance(middleware_cache.caches, CacheHandlerPatch): |
| 261 | + middleware_cache.caches = cache.caches |
283 | 262 |
|
284 | 263 | # Wrap the patched cache inside Django's ConnectionProxy |
285 | 264 | if ConnectionProxy: |
286 | 265 | cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS) |
287 | 266 |
|
288 | 267 | 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. |
| 268 | + for alias in cache.caches: |
| 269 | + if isinstance(cache.caches[alias], CacheStatTracker): |
| 270 | + cache.caches[alias] = cache.caches[alias].cache |
| 271 | + if ConnectionProxy: |
| 272 | + cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS) |
| 273 | + # While it can be restored to the original, any views that were |
| 274 | + # wrapped with the cache_page decorator will continue to use a |
| 275 | + # monkey patched cache. |
305 | 276 |
|
306 | 277 | def generate_stats(self, request, response): |
307 | 278 | self.record_stats( |
|
0 commit comments