23 lines
557 B
Python
23 lines
557 B
Python
import json
|
|
import os
|
|
|
|
CACHE_DIR = os.path.join(os.path.dirname(__file__), 'cache')
|
|
|
|
|
|
def _cache_path(key: str) -> str:
|
|
safe_key = key.replace('/', '_').replace(' ', '_')
|
|
return os.path.join(CACHE_DIR, f"{safe_key}.json")
|
|
|
|
|
|
def get_cached(key: str):
|
|
path = _cache_path(key)
|
|
if not os.path.exists(path):
|
|
return None
|
|
with open(path) as f:
|
|
return json.load(f)
|
|
|
|
|
|
def set_cached(key: str, data) -> None:
|
|
os.makedirs(CACHE_DIR, exist_ok=True)
|
|
with open(_cache_path(key), 'w') as f:
|
|
json.dump(data, f, indent=2)
|