def cache_response(self, request, response, body=None, status_codes=None):
<EOL>cacheable_status_codes = status_codes or self.cacheable_status_codes<EOL>if response.status not in cacheable_status_codes:<EOL><INDENT>logger.debug(<EOL>"<STR_LIT>", response.status, cacheable_status_codes<EOL>)<EOL>return<EOL><DEDENT>response_headers = CaseInsensitiveDict(response.headers)<EOL>if (<EOL>body is not None<EOL>and "<STR_LIT>" in response_headers<EOL>and response_headers["<STR_LIT>"].isdigit()<EOL>and int(response_headers["<STR_LIT>"]) != len(body)<EOL>):<EOL><INDENT>return<EOL><DEDENT>cc_req = self.parse_cache_control(request.headers)<EOL>cc = self.parse_cache_control(response_headers)<EOL>cache_url = self.cache_url(request.url)<EOL>logger.debug('<STR_LIT>', cache_url)<EOL>no_store = False<EOL>if "<STR_LIT>" in cc:<EOL><INDENT>no_store = True<EOL>logger.debug('<STR_LIT>')<EOL><DEDENT>if "<STR_LIT>" in cc_req:<EOL><INDENT>no_store = True<EOL>logger.debug('<STR_LIT>')<EOL><DEDENT>if no_store and self.cache.get(cache_url):<EOL><INDENT>logger.debug('<STR_LIT>')<EOL>self.cache.delete(cache_url)<EOL><DEDENT>if no_store:<EOL><INDENT>return<EOL><DEDENT>if self.cache_etags and "<STR_LIT>" in response_headers:<EOL><INDENT>logger.debug("<STR_LIT>")<EOL>self.cache.set(<EOL>cache_url, self.serializer.dumps(request, response, body=body)<EOL>)<EOL><DEDENT>elif response.status == <NUM_LIT>:<EOL><INDENT>logger.debug("<STR_LIT>")<EOL>self.cache.set(cache_url, self.serializer.dumps(request, response))<EOL><DEDENT>elif "<STR_LIT:date>" in response_headers:<EOL><INDENT>if "<STR_LIT>" in cc and cc["<STR_LIT>"] > <NUM_LIT:0>:<EOL><INDENT>logger.debug("<STR_LIT>")<EOL>self.cache.set(<EOL>cache_url, self.serializer.dumps(request, response, body=body)<EOL>)<EOL><DEDENT>elif "<STR_LIT>" in response_headers:<EOL><INDENT>if response_headers["<STR_LIT>"]:<EOL><INDENT>logger.debug("<STR_LIT>")<EOL>self.cache.set(<EOL>cache_url, self.serializer.dumps(request, response, body=body)<EOL>)<EOL><DEDENT><DEDENT><DEDENT>
Algorithm for caching requests.

This assumes a requests Response object.