flowmarg / mcp /cache_manager.py
akash
Deploy to Hugging Face (source code only, no binary files)
2379054
Raw History Blame Contribute Delete
2.55 kB
"""
CACHE MANAGER - Query Caching for Speed
"""
import json
import hashlib
from pathlib import Path
from datetime import datetime, timedelta
import threading
class CacheManager:
def __init__(self, cache_dir=".cache"):
self.cache_dir = Path(cache_dir)
self.cache_dir.mkdir(exist_ok=True)
self.cache = {}
self.lock = threading.Lock()
self._load_cache()
def _get_cache_key(self, question: str, top_k: int = None) -> str:
key_string = question.lower().strip()
if top_k:
key_string += f"_k{top_k}"
return hashlib.md5(key_string.encode()).hexdigest()
def _load_cache(self):
cache_file = self.cache_dir / "cache.json"
if cache_file.exists():
try:
with open(cache_file, 'r') as f:
self.cache = json.load(f)
except Exception as e:
self.cache = {}
def _save_cache(self):
cache_file = self.cache_dir / "cache.json"
try:
now = datetime.now()
self.cache = {
k: v for k, v in self.cache.items()
if datetime.fromisoformat(v.get('timestamp', '2000-01-01')) > now - timedelta(days=7)
}
with open(cache_file, 'w') as f:
json.dump(self.cache, f)
except Exception as e:
pass
def get(self, question: str, top_k: int = None):
key = self._get_cache_key(question, top_k)
with self.lock:
if key in self.cache:
entry = self.cache[key]
try:
timestamp = datetime.fromisoformat(entry.get('timestamp', '2000-01-01'))
if datetime.now() - timestamp < timedelta(days=7):
return entry.get('data')
except Exception as e:
pass
return None
def set(self, question: str, data, top_k: int = None):
key = self._get_cache_key(question, top_k)
with self.lock:
self.cache[key] = {
'data': data,
'timestamp': datetime.now().isoformat(),
'question': question
}
self._save_cache()
def clear(self):
with self.lock:
self.cache = {}
self._save_cache()
def stats(self) -> dict:
return {
'total_entries': len(self.cache),
'cache_size_kb': len(str(self.cache)) // 1024
}
_cache = CacheManager()