Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cachecontrol/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def __init__(
self.cache_etags = cache_etags
self.serializer = serializer or Serializer()
self.cacheable_status_codes = status_codes or (200, 203, 300, 301, 308)
self._request_to_cache_response = weakref.WeakKeyDictionary()

@classmethod
def _urlnorm(cls, uri: str) -> str:
Expand Down Expand Up @@ -281,6 +282,10 @@ def conditional_headers(self, request: PreparedRequest) -> dict[str, str]:
new_headers = {}

if resp:
# Save the cached response associated with the request so
# if the server returns a 304 we can use exactly this response.
# (usefull in case the cache gets altered in the meantime)
self._request_to_cache_response[request] = resp
headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(resp.headers)

if "etag" in headers:
Expand Down Expand Up @@ -479,7 +484,7 @@ def update_cached_response(
"""
assert request.url is not None
cache_url = self.cache_url(request.url)
cached_response = self._load_from_cache(request)
cached_response = self._request_to_cache_response.get(request) or self._load_from_cache(request)

if not cached_response:
# we didn't have a cached response
Expand Down