-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreading_article_cache.py
More file actions
63 lines (48 loc) · 1.67 KB
/
Copy pathreading_article_cache.py
File metadata and controls
63 lines (48 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from __future__ import annotations
import os
from pathlib import Path
import time
DEFAULT_TTL_HOURS = 72
def cache_dir() -> Path | None:
raw = os.environ.get("READING_ARTICLE_CACHE_DIR", "").strip()
return Path(raw) if raw else None
def _path(item_id: str) -> Path | None:
root = cache_dir()
if root is None:
return None
safe = "".join(ch for ch in item_id if ch.isalnum() or ch in "-_")
return root / f"{safe}.txt"
def cleanup_expired(*, ttl_hours: int = DEFAULT_TTL_HOURS) -> None:
root = cache_dir()
if root is None or not root.exists():
return
cutoff = time.time() - max(1, ttl_hours) * 3600
for path in root.glob("*.txt"):
try:
if path.stat().st_mtime < cutoff:
path.unlink(missing_ok=True)
except OSError:
continue
def put_article(item_id: str, body: str) -> None:
path = _path(item_id)
text = body.strip()
if path is None or not text:
return
path.parent.mkdir(parents=True, exist_ok=True)
cleanup_expired()
tmp = path.with_name(f"{path.name}.{os.getpid()}.tmp")
tmp.write_text(text, encoding="utf-8")
os.replace(tmp, path)
def get_article(item_id: str) -> str:
path = _path(item_id)
if path is None or not path.exists():
return ""
ttl = int(os.environ.get("READING_ARTICLE_CACHE_TTL_HOURS", str(DEFAULT_TTL_HOURS)))
if path.stat().st_mtime < time.time() - max(1, ttl) * 3600:
path.unlink(missing_ok=True)
return ""
return path.read_text(encoding="utf-8").strip()
def delete_article(item_id: str) -> None:
path = _path(item_id)
if path is not None:
path.unlink(missing_ok=True)