Skip to content

Commit 0763108

Browse files
committed
Add some more debug logging for FileCache and for the pass-through path.
This makes it easier to figure out _why_ something fails to look up altogether.
1 parent fcc7103 commit 0763108

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

cachecontrol/caches/file_cache.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import hashlib
6+
import logging
67
import os
78
from textwrap import dedent
89

@@ -16,6 +17,9 @@
1617
FileNotFoundError = (IOError, OSError)
1718

1819

20+
logger = logging.getLogger(__name__)
21+
22+
1923
def _secure_open_write(filename, fmode):
2024
# We only want to write to this file, so open it in write only mode
2125
flags = os.O_WRONLY
@@ -111,6 +115,7 @@ def _fn(self, name):
111115

112116
def get(self, key):
113117
name = self._fn(key)
118+
logger.debug("Looking up '%s' in '%s'", key, name)
114119
try:
115120
with open(name, "rb") as fh:
116121
return fh.read()
@@ -120,12 +125,14 @@ def get(self, key):
120125

121126
def set(self, key, value):
122127
name = self._fn(key)
128+
logger.debug("Caching '%s' in '%s'", key, name)
123129

124130
# Make sure the directory exists
131+
parentdir = os.path.dirname(name)
125132
try:
126-
os.makedirs(os.path.dirname(name), self.dirmode)
133+
os.makedirs(parentdir, self.dirmode)
127134
except (IOError, OSError):
128-
pass
135+
logging.debug("Error trying to create directory '%s'", parentdir, exc_info=True)
129136

130137
with self.lock_class(name) as lock:
131138
# Write our actual file

cachecontrol/controller.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def cache_response(self, request, response, body=None, status_codes=None):
284284
cc = self.parse_cache_control(response_headers)
285285

286286
cache_url = self.cache_url(request.url)
287-
logger.debug('Updating cache with response from "%s"', cache_url)
287+
logger.debug('Updating cache %r with response from "%s"', self.cache, cache_url)
288288

289289
# Delete it from the cache if we happen to have it stored there
290290
no_store = False
@@ -325,7 +325,10 @@ def cache_response(self, request, response, body=None, status_codes=None):
325325
# Add to the cache if the response headers demand it. If there
326326
# is no date header then we can't do anything about expiring
327327
# the cache.
328-
elif "date" in response_headers:
328+
elif "date" not in response_headers:
329+
logger.debug("No date header, expiration cannot be set.")
330+
return
331+
else:
329332
# cache when there is a max-age > 0
330333
if "max-age" in cc and cc["max-age"] > 0:
331334
logger.debug("Caching b/c date exists and max-age > 0")
@@ -341,6 +344,8 @@ def cache_response(self, request, response, body=None, status_codes=None):
341344
self.cache.set(
342345
cache_url, self.serializer.dumps(request, response, body)
343346
)
347+
else:
348+
logger.debug("No combination of headers to cache.")
344349

345350
def update_cached_response(self, request, response):
346351
"""On a 304 we will get a new set of headers that we want to

0 commit comments

Comments
 (0)