'Algorithm for caching requests.
This assumes a requests Response object.'

def cache_response(self, request, response, body=None):

    if (response.status not in [200, 203, 300, 301]):
        return
    response_headers = CaseInsensitiveDict(response.headers)
    cc_req = self.parse_cache_control(request.headers)
    cc = self.parse_cache_control(response_headers)
    cache_url = self.cache_url(request.url)
    no_store = (cc.get('no-store') or cc_req.get('no-store'))
    if (no_store and self.cache.get(cache_url)):
        self.cache.delete(cache_url)
    if (self.cache_etags and ('etag' in response_headers)):
        self.cache.set(cache_url, self.serializer.dumps(request, response, body=body))
    elif (response.status == 301):
        self.cache.set(cache_url, self.serializer.dumps(request, response))
    elif ('date' in response_headers):
        if (cc and cc.get('max-age')):
            if (int(cc['max-age']) > 0):
                self.cache.set(cache_url, self.serializer.dumps(request, response, body=body))
        elif ('expires' in response_headers):
            if response_headers['expires']:
                self.cache.set(cache_url, self.serializer.dumps(request, response, body=body))
