|
5 | 5 |
|
6 | 6 | from django.conf import settings |
7 | 7 | from django.core import cache |
8 | | -from django.core.cache import CacheHandler, caches as original_caches |
| 8 | +from django.core.cache import ( |
| 9 | + DEFAULT_CACHE_ALIAS, |
| 10 | + CacheHandler, |
| 11 | + cache as original_cache, |
| 12 | + caches as original_caches, |
| 13 | +) |
9 | 14 | from django.core.cache.backends.base import BaseCache |
10 | 15 | from django.dispatch import Signal |
11 | 16 | from django.middleware import cache as middleware_cache |
@@ -130,20 +135,12 @@ def decr_version(self, *args, **kwargs): |
130 | 135 | return self.cache.decr_version(*args, **kwargs) |
131 | 136 |
|
132 | 137 |
|
133 | | -# class CacheHandlerPatch(CacheHandler): |
134 | | -# def ____getitem__(self, alias): |
135 | | -# actual_cache = super().__getitem__(alias) |
136 | | -# return CacheStatTracker(actual_cache) |
137 | | -# |
138 | | -# def create_connection(self, alias): |
139 | | -# return CacheStatTracker(super().create_connection(alias)) |
140 | | -# |
141 | | -# |
142 | | -# middleware_cache.caches = CacheHandlerPatch() |
| 138 | +class CacheHandlerPatch(CacheHandler): |
| 139 | + def create_connection(self, alias): |
| 140 | + return CacheStatTracker(super().create_connection(alias)) |
143 | 141 |
|
144 | 142 |
|
145 | | -original_caches._original_create_connection = original_caches.create_connection |
146 | | -original_caches.create_connection = lambda alias: CacheStatTracker(original_caches._original_create_connection(alias)) |
| 143 | +middleware_cache.caches = CacheHandlerPatch() |
147 | 144 |
|
148 | 145 |
|
149 | 146 | class CachePanel(Panel): |
@@ -248,19 +245,18 @@ def title(self): |
248 | 245 | ) |
249 | 246 |
|
250 | 247 | def enable_instrumentation(self): |
251 | | - return |
252 | | - if isinstance(middleware_cache.caches, CacheHandlerPatch): |
253 | | - cache.caches = middleware_cache.caches |
254 | | - else: |
255 | | - cache.caches = CacheHandlerPatch() |
| 248 | + if not isinstance(middleware_cache.caches, CacheHandlerPatch): |
| 249 | + middleware_cache.caches = CacheHandlerPatch() |
| 250 | + cache.caches = middleware_cache.caches |
| 251 | + cache.cache = cache.caches[DEFAULT_CACHE_ALIAS] |
256 | 252 |
|
257 | 253 | def disable_instrumentation(self): |
258 | | - return |
259 | | - cache.caches = original_caches |
260 | 254 | # While it can be restored to the original, any views that were |
261 | 255 | # wrapped with the cache_page decorator will continue to use a |
262 | 256 | # monkey patched cache. |
263 | 257 | middleware_cache.caches = original_caches |
| 258 | + cache.caches = original_caches |
| 259 | + cache.cache = original_cache |
264 | 260 |
|
265 | 261 | def generate_stats(self, request, response): |
266 | 262 | self.record_stats( |
|
0 commit comments