Skip to content

Commit 91b3e11

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 08be136 commit 91b3e11

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
@@ -1,4 +1,5 @@
11
import hashlib
2+
import logging
23
import os
34
from textwrap import dedent
45

@@ -12,6 +13,9 @@
1213
FileNotFoundError = (IOError, OSError)
1314

1415

16+
logger = logging.getLogger(__name__)
17+
18+
1519
def _secure_open_write(filename, fmode):
1620
# We only want to write to this file, so open it in write only mode
1721
flags = os.O_WRONLY
@@ -107,6 +111,7 @@ def _fn(self, name):
107111

108112
def get(self, key):
109113
name = self._fn(key)
114+
logger.debug("Looking up '%s' in '%s'", key, name)
110115
try:
111116
with open(name, "rb") as fh:
112117
return fh.read()
@@ -116,12 +121,14 @@ def get(self, key):
116121

117122
def set(self, key, value):
118123
name = self._fn(key)
124+
logger.debug("Caching '%s' in '%s'", key, name)
119125

120126
# Make sure the directory exists
127+
parentdir = os.path.dirname(name)
121128
try:
122-
os.makedirs(os.path.dirname(name), self.dirmode)
129+
os.makedirs(parentdir, self.dirmode)
123130
except (IOError, OSError):
124-
pass
131+
logging.debug("Error trying to create directory '%s'", parentdir, exc_info=True)
125132

126133
with self.lock_class(name) as lock:
127134
# Write our actual file

cachecontrol/controller.py

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

282282
cache_url = self.cache_url(request.url)
283-
logger.debug('Updating cache with response from "%s"', cache_url)
283+
logger.debug('Updating cache %r with response from "%s"', self.cache, cache_url)
284284

285285
# Delete it from the cache if we happen to have it stored there
286286
no_store = False
@@ -321,7 +321,10 @@ def cache_response(self, request, response, body=None, status_codes=None):
321321
# Add to the cache if the response headers demand it. If there
322322
# is no date header then we can't do anything about expiring
323323
# the cache.
324-
elif "date" in response_headers:
324+
elif "date" not in response_headers:
325+
logger.debug("No date header, expiration cannot be set.")
326+
return
327+
else:
325328
# cache when there is a max-age > 0
326329
if "max-age" in cc and cc["max-age"] > 0:
327330
logger.debug("Caching b/c date exists and max-age > 0")
@@ -337,6 +340,8 @@ def cache_response(self, request, response, body=None, status_codes=None):
337340
self.cache.set(
338341
cache_url, self.serializer.dumps(request, response, body=body)
339342
)
343+
else:
344+
logger.debug("No combination of headers to cache.")
340345

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

0 commit comments

Comments
 (0)