Skip to content

Commit

Permalink
better fix for last issue
Browse files Browse the repository at this point in the history
  • Loading branch information
willforde committed Feb 27, 2021
1 parent c970960 commit 3b39871
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions urlquick.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,11 @@ def wipe(self):
# noinspection PyShadowingNames
def send(self, request, **kwargs): # type: (PreparedRequest, ...) -> Response
max_age = int(request.headers.pop("x-cache-max-age"))
urlhash = hash_url(request)
urlhash = hash_url(request) if max_age >= 0 else None
cache = None

# Check if request is already cached and valid
if max_age >= 0 and request.method in CACHEABLE_METHODS:
if urlhash and request.method in CACHEABLE_METHODS:
cache = self.get_cache(urlhash, max_age)
if cache and cache.isfresh:
logger.debug("Cache is fresh")
Expand All @@ -363,15 +363,14 @@ def send(self, request, **kwargs): # type: (PreparedRequest, ...) -> Response

# Send request for remote resource
response = super(CacheHTTPAdapter, self).send(request, **kwargs)
return self.process_response(response, cache, urlhash, max_age)
return self.process_response(response, cache, urlhash) if urlhash else response

def build_response(self, req, resp): # type: (PreparedRequest, HTTPResponse) -> Response
"""Replace response object with our customized version."""
resp = super(CacheHTTPAdapter, self).build_response(req, resp)
return Response.extend_response(resp)

def process_response(self, response, cache, urlhash, max_age):
# type: (Response, CacheRecord, str, int) -> Response
def process_response(self, response, cache, urlhash): # type: (Response, CacheRecord, str) -> Response
"""Save response to cache if possible."""
# Check for Not Modified response
if cache and response.status_code == codes.not_modified:
Expand All @@ -381,7 +380,7 @@ def process_response(self, response, cache, urlhash, max_age):
response = cache.response

# Cache any cacheable responses
elif max_age >= 0 and response.request.method in CACHEABLE_METHODS and response.status_code in CACHEABLE_CODES:
elif response.request.method in CACHEABLE_METHODS and response.status_code in CACHEABLE_CODES:
logger.debug("Caching %s %s response", response.status_code, response.reason)
response = self.set_cache(urlhash, response)

Expand Down

0 comments on commit 3b39871

Please sign in to comment.