Properly re-load the fetched cache data

This commit is contained in:
Nexus 2024-02-10 01:53:09 +00:00
parent fb0a57b7d0
commit 8ed5c8e498
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -1,5 +1,7 @@
import io
import json
import os
import time
import uuid
import fastapi
@ -203,13 +205,13 @@ def preview_url(
metadata, _ts = result
if ts is None or abs(ts - _ts) < 3600:
logging.debug("Optimal cache hit for %r", url)
return metadata
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 metadata
return json.loads(metadata)
domain = os.environ.get("PREVIEW_HOMESERVER", "https://" + req.url.hostname)
@ -308,7 +310,7 @@ def preview_url(
with sqlite3.connect(CACHE_FILE) as conn:
conn.execute(
"INSERT INTO cache (uuid, url, ts, metadata) VALUES (?, ?, ?, ?)",
(str(uuid.uuid4()), url, int(response.headers["date"]), str(og_tags))
(str(uuid.uuid4()), url, round(time.time()), json.dumps(og_tags))
)
return og_tags