-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_manager.py
More file actions
146 lines (128 loc) · 4.65 KB
/
Copy pathcache_manager.py
File metadata and controls
146 lines (128 loc) · 4.65 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from __future__ import annotations
import hashlib
import json
import time
from pathlib import Path
from typing import Optional, Dict, Any
import redis
class CacheManager:
def __init__(self, use_redis: bool = True, redis_host: str = "redis", redis_port: int = 6379):
self.use_redis = use_redis
if use_redis:
try:
self.redis_client = redis.Redis(
host=redis_host,
port=redis_port,
db=0,
decode_responses=True,
socket_connect_timeout=5
)
self.redis_client.ping()
print("✅ Redis cache connected")
except:
print("⚠️ Redis unavailable, using file cache")
self.use_redis = False
self._init_file_cache()
else:
self._init_file_cache()
def _init_file_cache(self):
self.cache_dir = Path("cache_data")
self.cache_dir.mkdir(exist_ok=True)
self.cache_file = self.cache_dir / "query_cache.json"
if self.cache_file.exists():
try:
with open(self.cache_file, "r", encoding="utf-8") as f:
self.file_cache = json.load(f)
except:
self.file_cache = {}
else:
self.file_cache = {}
def _generate_key(self, query: str, model_name: str) -> str:
combined = f"{model_name}:{query.strip().lower()}"
return hashlib.sha256(combined.encode()).hexdigest()
def get(self, query: str, model_name: str) -> Optional[Dict[str, Any]]:
key = self._generate_key(query, model_name)
if self.use_redis:
try:
cached = self.redis_client.get(key)
if cached:
data = json.loads(cached)
if time.time() - data["timestamp"] < data["ttl"]:
return data["response"]
else:
self.redis_client.delete(key)
return None
except:
return None
else:
if key in self.file_cache:
data = self.file_cache[key]
if time.time() - data["timestamp"] < data["ttl"]:
return data["response"]
else:
del self.file_cache[key]
self._save_file_cache()
return None
return None
def set(self, query: str, model_name: str, response: Dict[str, Any], ttl: int = 3600):
key = self._generate_key(query, model_name)
cache_data = {
"response": response,
"timestamp": time.time(),
"ttl": ttl
}
if self.use_redis:
try:
self.redis_client.setex(
key,
ttl,
json.dumps(cache_data)
)
except:
pass
else:
self.file_cache[key] = cache_data
self._save_file_cache()
def _save_file_cache(self):
try:
with open(self.cache_file, "w", encoding="utf-8") as f:
json.dump(self.file_cache, f, ensure_ascii=False, indent=2)
except:
pass
def clear(self):
if self.use_redis:
try:
self.redis_client.flushdb()
except:
pass
else:
self.file_cache = {}
self._save_file_cache()
def get_stats(self) -> Dict[str, Any]:
if self.use_redis:
try:
info = self.redis_client.info()
return {
"type": "redis",
"total_keys": self.redis_client.dbsize(),
"memory_used": info.get("used_memory_human", "N/A"),
"hits": info.get("keyspace_hits", 0),
"misses": info.get("keyspace_misses", 0)
}
except:
return {"type": "redis", "status": "error"}
else:
valid_keys = 0
expired_keys = 0
current_time = time.time()
for data in self.file_cache.values():
if current_time - data["timestamp"] < data["ttl"]:
valid_keys += 1
else:
expired_keys += 1
return {
"type": "file",
"total_keys": len(self.file_cache),
"valid_keys": valid_keys,
"expired_keys": expired_keys
}