-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjangocache.py
219 lines (178 loc) · 7.66 KB
/
djangocache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import contextlib
import time
from django.conf import settings
from django.core.cache.backends.dummy import DummyCache
from django.middleware import cache as cache_middleware
from django.utils import http, cache, decorators
__all__ = ['cache_page']
dummy_cache = DummyCache('dummy_host', {})
# https://tools.ietf.org/html/rfc7232#section-4.1
rfc7232_headers = ['ETag', 'Vary', 'Cache-Control', 'Expires', 'Content-Location', 'Date', 'Last-Modified']
def cache_page(**kwargs):
"""
This decorator is similar to `django.views.decorators.cache.cache_page`
"""
cache_timeout = kwargs.pop('cache_timeout', None)
key_prefix = kwargs.pop('key_prefix', None)
cache_min_age = kwargs.pop('cache_min_age', None)
decorator = decorators.decorator_from_middleware_with_args(CacheMiddleware)(
cache_timeout=cache_timeout,
key_prefix=key_prefix,
cache_min_age=cache_min_age,
**kwargs
)
return decorator
@contextlib.contextmanager
def patch(obj, attr, value, default=None):
original = getattr(obj, attr, default)
setattr(obj, attr, value)
yield
setattr(obj, attr, original)
def get_cache_max_age(cache_control):
if not cache_control:
return
cache_control_kwargs = dict(
cache._to_tuple(attr)
for attr in
cache.cc_delim_re.split(cache_control)
)
if 'max-age' in cache_control_kwargs:
try:
return int(cache_control_kwargs['max-age'])
except (ValueError, TypeError):
pass
def get_conditional_response(request, response=None):
if not (response and hasattr(cache, 'get_conditional_response')):
# Django 1.8 does not have such method, can't do anything
return response
last_modified = response.get('Last-Modified')
conditional_response = cache.get_conditional_response(
request,
last_modified=http.parse_http_date_safe(last_modified),
response=response,
)
if conditional_response is response:
return response
headers = {
header: response[header]
for header in rfc7232_headers
if header in response
}
for header, value in headers.items():
conditional_response[header] = value
return conditional_response
class ResponseCacheUpdater(object):
def __init__(self, middleware, request, response):
self.middleware = middleware
self.request = request
self.response = response
def close(self):
middleware = self.middleware
request = self.request
response = self.response
self.request = self.response = self.middleware = None
with patch(response, '_closable_objects', []):
# do not save _closable_objects to cache
self.update_cache(middleware, request, response)
@staticmethod
def update_cache(middleware, request, response):
cache_timeout = getattr(request, '_cache_timeout', None)
key_prefix = getattr(request, '_cache_key_prefix', None)
with patch(cache_middleware, 'patch_response_headers', lambda *_: None):
# we do not want patch response again
with patch(middleware, 'key_prefix', key_prefix):
with patch(middleware, 'cache_timeout', cache_timeout):
super(CacheMiddleware, middleware).process_response(
request, response,
)
class CacheMiddleware(cache_middleware.CacheMiddleware):
"""
Despite of the original one this middleware supports
callable 'key_prefix' and 'cache_timeout'
"""
CONDITIONAL_VARY_HEADERS = {
'HTTP_IF_NONE_MATCH': 'If-None-Match',
'HTTP_IF_MATCH': 'If-Match',
}
def __init__(self, cache_min_age=None, *args, **kwargs):
self.cache_min_age = cache_min_age
super(CacheMiddleware, self).__init__(*args, **kwargs)
if callable(self.key_prefix):
self.get_key_prefix = self.key_prefix
if callable(self.cache_timeout):
self.get_cache_timeout = self.cache_timeout
def get_cache_timeout(self, request, *args, **kwargs):
return self.cache_timeout
def get_key_prefix(self, request, *args, **kwargs):
return self.key_prefix
def process_request(self, request):
request._cache_key_prefix = key_prefix = self.get_key_prefix(
request,
*request.resolver_match.args,
**request.resolver_match.kwargs
)
with patch(self, 'key_prefix', key_prefix):
response = super(CacheMiddleware, self).process_request(request)
# check if we should return "304 Not Modified"
response = response and get_conditional_response(request, response)
# setting cache age
if response and 'Expires' in response:
max_age = get_cache_max_age(response.get('Cache-Control'))
if max_age:
expires = http.parse_http_date(response['Expires'])
timeout = expires - int(time.time())
response['Age'] = age = max_age - timeout
# check cache age limit provided by client
age_limit = get_cache_max_age(request.META.get('HTTP_CACHE_CONTROL'))
if age_limit is None and request.META.get('HTTP_PRAGMA') == 'no-cache':
age_limit = 0
if age_limit is not None:
min_age = self.cache_min_age
if min_age is None:
min_age = getattr(settings, 'DJANGOCACHE_MIN_AGE', 0)
age_limit = max(min_age, age_limit)
if age >= age_limit:
request._cache_update_cache = True
return None
return response
def process_response(self, request, response):
if not self._should_update_cache(request, response):
return super(CacheMiddleware, self).process_response(request, response)
last_modified = 'Last-Modified' in response
etag = 'ETag' in response
request._cache_timeout = cache_timeout = self.get_cache_timeout(
request,
*request.resolver_match.args,
**request.resolver_match.kwargs
)
conditional_vary_headers = [
http_header
for wsgi_header, http_header in self.CONDITIONAL_VARY_HEADERS.items()
if wsgi_header in request.META
]
if conditional_vary_headers:
cache.patch_vary_headers(response, conditional_vary_headers)
if response.status_code == 304: # Not Modified
cache.patch_response_headers(response, cache_timeout)
else:
update_response_cache = ResponseCacheUpdater(
middleware=self,
request=request,
response=response,
)
response._closable_objects.append(update_response_cache)
with patch(cache_middleware, 'learn_cache_key', lambda *_, **__: ''):
# replace learn_cache_key with dummy one
with patch(self, 'cache', dummy_cache):
# use dummy_cache to postpone cache update till the time
# when all values of Vary header are ready,
# see https://code.djangoproject.com/ticket/15855
with patch(self, 'cache_timeout', cache_timeout):
response = super(CacheMiddleware, self).process_response(request, response)
if not last_modified:
# patch_response_headers sets its own Last-Modified, remove it
del response['Last-Modified']
if not etag:
# patch_response_headers sets its own ETag, remove it
del response['ETag']
return response