-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
45 lines (34 loc) · 1.05 KB
/
Copy pathcache.py
File metadata and controls
45 lines (34 loc) · 1.05 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
import hashlib
from datetime import datetime, timedelta
from config import config
_cache: dict = {}
def _make_key(topic: str, depth: str) -> str:
raw = f"{topic.lower().strip()}:{depth}"
return hashlib.md5(raw.encode()).hexdigest()
def get_cached(topic: str, depth: str) -> dict | None:
key = _make_key(topic, depth)
entry = _cache.get(key)
if not entry:
return None
if datetime.now() > entry["expires"]:
del _cache[key]
return None
return entry["result"]
def set_cache(topic: str, depth: str, result: dict) -> None:
key = _make_key(topic, depth)
_cache[key] = {
"result": result,
"expires": datetime.now() + timedelta(seconds=config.CACHE_TTL)
}
def cache_stats() -> dict:
now = datetime.now()
valid = [k for k, v in _cache.items() if v["expires"] > now]
return {
"total_entries": len(_cache),
"valid_entries": len(valid),
"ttl_seconds": config.CACHE_TTL
}
def clear_cache() -> int:
count = len(_cache)
_cache.clear()
return count