@@ -122,6 +122,26 @@ def parse_cache_control(self, headers):
122
122
123
123
return retval
124
124
125
+ def _load_from_cache (self , request ):
126
+ """
127
+ Load a cached response, or return None if it's not available.
128
+ """
129
+ cache_url = request .url
130
+ cache_data = self .cache .get (cache_url )
131
+ if cache_data is None :
132
+ logger .debug ("No cache entry available" )
133
+ return None
134
+
135
+ if isinstance (self .cache , SeparateBodyBaseCache ):
136
+ body_file = self .cache .get_body (cache_url )
137
+ else :
138
+ body_file = None
139
+
140
+ result = self .serializer .loads (request , cache_data , body_file )
141
+ if result is None :
142
+ logger .warning ("Cache entry deserialization failed, entry ignored" )
143
+ return result
144
+
125
145
def cached_request (self , request ):
126
146
"""
127
147
Return a cached response if it exists in the cache, otherwise
@@ -140,21 +160,9 @@ def cached_request(self, request):
140
160
logger .debug ('Request header has "max_age" as 0, cache bypassed' )
141
161
return False
142
162
143
- # Request allows serving from the cache, let's see if we find something
144
- cache_data = self .cache .get (cache_url )
145
- if cache_data is None :
146
- logger .debug ("No cache entry available" )
147
- return False
148
-
149
- if isinstance (self .cache , SeparateBodyBaseCache ):
150
- body_file = self .cache .get_body (cache_url )
151
- else :
152
- body_file = None
153
-
154
- # Check whether it can be deserialized
155
- resp = self .serializer .loads (request , cache_data , body_file )
163
+ # Check whether we can load the response from the cache:
164
+ resp = self ._load_from_cache (request )
156
165
if not resp :
157
- logger .warning ("Cache entry deserialization failed, entry ignored" )
158
166
return False
159
167
160
168
# If we have a cached permanent redirect, return it immediately. We
@@ -240,8 +248,7 @@ def cached_request(self, request):
240
248
return False
241
249
242
250
def conditional_headers (self , request ):
243
- cache_url = self .cache_url (request .url )
244
- resp = self .serializer .loads (request , self .cache .get (cache_url ))
251
+ resp = self ._load_from_cache (request )
245
252
new_headers = {}
246
253
247
254
if resp :
@@ -267,7 +274,10 @@ def _cache_set(self, cache_url, request, response, body=None, expires_time=None)
267
274
self .serializer .dumps (request , response , b"" ),
268
275
expires = expires_time ,
269
276
)
270
- self .cache .set_body (cache_url , body )
277
+ # body is None can happen when, for example, we're only updating
278
+ # headers, as is the case in update_cached_response().
279
+ if body is not None :
280
+ self .cache .set_body (cache_url , body )
271
281
else :
272
282
self .cache .set (
273
283
cache_url ,
@@ -406,18 +416,7 @@ def update_cached_response(self, request, response):
406
416
gotten a 304 as the response.
407
417
"""
408
418
cache_url = self .cache_url (request .url )
409
-
410
- # NOTE: This is a hot-patch for
411
- # https://github.com/ionrock/cachecontrol/issues/276 until it's fixed
412
- # upstream.
413
- if isinstance (self .cache , SeparateBodyBaseCache ):
414
- body_file = self .cache .get_body (cache_url )
415
- else :
416
- body_file = None
417
-
418
- cached_response = self .serializer .loads (
419
- request , self .cache .get (cache_url ), body_file
420
- )
419
+ cached_response = self ._load_from_cache (request )
421
420
422
421
if not cached_response :
423
422
# we didn't have a cached response
0 commit comments