Log cache hits and misses

This commit is contained in:
Nexus 2024-02-22 18:27:53 +00:00
parent d7422926ad
commit 8e8b460975
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -224,12 +224,18 @@ def preview_url(
if ts is None or abs(ts - _ts) < 3600: if ts is None or abs(ts - _ts) < 3600:
logging.debug("Optimal cache hit for %r", url) logging.debug("Optimal cache hit for %r", url)
return json.loads(metadata) return json.loads(metadata)
else:
logging.debug("No optimal cache matches for url %r.", url)
# No close matches, get the latest one # No close matches, get the latest one
metadata, _ts = results[-1] metadata, _ts = results[-1]
# If the latest one is more than 3 hours old, re-fetch. Otherwise, return. # If the latest one is more than a week old, re-fetch. Otherwise, return.
if ts is None or abs(ts - _ts) < 10800: if ts is None or abs(ts - _ts) < 604800:
logging.debug("Cache hit for %r", url) logging.debug("Stale cache hit for %r", url)
return json.loads(metadata) return json.loads(metadata)
else:
logging.debug("Cache miss for %r")
else:
logging.debug("Full cache miss for %r", url)
domain = os.environ.get("PREVIEW_HOMESERVER", "https://" + req.url.hostname) domain = os.environ.get("PREVIEW_HOMESERVER", "https://" + req.url.hostname)
with lock: with lock: