-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
42 lines (34 loc) · 898 Bytes
/
Copy pathcache.py
File metadata and controls
42 lines (34 loc) · 898 Bytes
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
import json
import os
import time
CACHE_TTL = 6 * 60 * 60
CACHE_FILE = os.path.join(os.path.dirname(__file__), "route_cache.json")
_cache = {}
def load_cache():
global _cache
if os.path.exists(CACHE_FILE):
try:
with open(CACHE_FILE, "r") as f:
_cache = json.load(f)
except Exception:
_cache = {}
def save_cache():
try:
with open(CACHE_FILE, "w") as f:
json.dump(_cache, f)
except Exception:
pass
def get_cached_profile(hostname: str):
entry = _cache.get(hostname)
if not entry:
return None
if time.time() - entry["ts"] > CACHE_TTL:
_cache.pop(hostname, None)
return None
return entry["profile"]
def set_cached_profile(hostname: str, profile: str):
_cache[hostname] = {
"profile": profile,
"ts": time.time()
}
save_cache()