55import re
66import calendar
77import time
8+ import traceback
89from email .utils import parsedate_tz
910
1011from requests .structures import CaseInsensitiveDict
@@ -280,7 +281,7 @@ def cache_response(self, request, response, body=None, status_codes=None):
280281 cc = self .parse_cache_control (response_headers )
281282
282283 cache_url = self .cache_url (request .url )
283- logger .debug ('Updating cache %r with response from "%s"' , self . cache , cache_url )
284+ logger .debug ('Updating cache with response from "%s"' , cache_url )
284285
285286 # Delete it from the cache if we happen to have it stored there
286287 no_store = False
@@ -309,14 +310,14 @@ def cache_response(self, request, response, body=None, status_codes=None):
309310 if self .cache_etags and "etag" in response_headers :
310311 logger .debug ("Caching due to etag" )
311312 self .cache .set (
312- cache_url , self .serializer .dumps (request , response , body = body )
313+ cache_url , self .serializer .dumps (request , response , body )
313314 )
314315
315316 # Add to the cache any permanent redirects. We do this before looking
316317 # that the Date headers.
317318 elif int (response .status ) in PERMANENT_REDIRECT_STATUSES :
318319 logger .debug ("Caching permanent redirect" )
319- self .cache .set (cache_url , self .serializer .dumps (request , response ))
320+ self .cache .set (cache_url , self .serializer .dumps (request , response , b'' ))
320321
321322 # Add to the cache if the response headers demand it. If there
322323 # is no date header then we can't do anything about expiring
@@ -329,7 +330,7 @@ def cache_response(self, request, response, body=None, status_codes=None):
329330 if "max-age" in cc and cc ["max-age" ] > 0 :
330331 logger .debug ("Caching b/c date exists and max-age > 0" )
331332 self .cache .set (
332- cache_url , self .serializer .dumps (request , response , body = body )
333+ cache_url , self .serializer .dumps (request , response , body )
333334 )
334335
335336 # If the request can expire, it means we should cache it
@@ -338,7 +339,7 @@ def cache_response(self, request, response, body=None, status_codes=None):
338339 if response_headers ["expires" ]:
339340 logger .debug ("Caching b/c of expires header" )
340341 self .cache .set (
341- cache_url , self .serializer .dumps (request , response , body = body )
342+ cache_url , self .serializer .dumps (request , response , body )
342343 )
343344 else :
344345 logger .debug ("No combination of headers to cache." )
@@ -379,6 +380,7 @@ def update_cached_response(self, request, response):
379380 cached_response .status = 200
380381
381382 # update our cache
382- self .cache .set (cache_url , self .serializer .dumps (request , cached_response ))
383+ body = cached_response .read (decode_content = False )
384+ self .cache .set (cache_url , self .serializer .dumps (request , cached_response , body ))
383385
384386 return cached_response
0 commit comments