File size: 944 Bytes
c397cfe | 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 | import json
import os
class APCache:
def __init__(self, file_path="data/cache.json"):
self.file_path = file_path
self.cache = {}
self._load()
def _load(self):
if os.path.exists(self.file_path):
try:
with open(self.file_path, 'r') as f:
self.cache = json.load(f)
except (json.JSONDecodeError, IOError):
self.cache = {}
def _save(self):
os.makedirs(os.path.dirname(self.file_path), exist_ok=True)
with open(self.file_path, 'w') as f:
json.dump(self.cache, f, indent=2)
def get(self, key):
return self.cache.get(key)
def set(self, key, value):
self.cache[key] = value
self._save()
def delete(self, key):
if key in self.cache:
del self.cache[key]
self._save()
def clear(self):
self.cache = {}
self._save() |