From 8e8b4609750fd10e2cb614ea1bb97b64fbda3321 Mon Sep 17 00:00:00 2001 From: nexy7574 Date: Thu, 22 Feb 2024 18:27:53 +0000 Subject: [PATCH] Log cache hits and misses --- server.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/server.py b/server.py index 948308e..f51142e 100644 --- a/server.py +++ b/server.py @@ -224,12 +224,18 @@ def preview_url( if ts is None or abs(ts - _ts) < 3600: logging.debug("Optimal cache hit for %r", url) return json.loads(metadata) - # No close matches, get the latest one - metadata, _ts = results[-1] - # If the latest one is more than 3 hours old, re-fetch. Otherwise, return. - if ts is None or abs(ts - _ts) < 10800: - logging.debug("Cache hit for %r", url) - return json.loads(metadata) + else: + logging.debug("No optimal cache matches for url %r.", url) + # No close matches, get the latest one + metadata, _ts = results[-1] + # If the latest one is more than a week old, re-fetch. Otherwise, return. + if ts is None or abs(ts - _ts) < 604800: + logging.debug("Stale cache hit for %r", url) + 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) with lock: