-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
200 lines (166 loc) · 6.16 KB
/
cache.py
File metadata and controls
200 lines (166 loc) · 6.16 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
CodeSense - Caching Mechanism
Thread-safe in-memory + disk-based LRU cache for analysis results.
"""
import hashlib
import json
import os
import time
import threading
from collections import OrderedDict
from pathlib import Path
from typing import Any, Optional
from logger import get_logger
logger = get_logger(__name__)
class LRUCache:
"""Thread-safe in-memory LRU cache with TTL support."""
def __init__(self, max_size: int = 500, ttl: int = 3600) -> None:
self.max_size = max_size
self.ttl = ttl
self._cache: OrderedDict[str, tuple[Any, float]] = OrderedDict()
self._lock = threading.Lock()
self.hits = 0
self.misses = 0
def _is_expired(self, timestamp: float) -> bool:
return (time.time() - timestamp) > self.ttl
def get(self, key: str) -> Optional[Any]:
with self._lock:
if key not in self._cache:
self.misses += 1
return None
value, ts = self._cache[key]
if self._is_expired(ts):
del self._cache[key]
self.misses += 1
return None
self._cache.move_to_end(key)
self.hits += 1
return value
def set(self, key: str, value: Any) -> None:
with self._lock:
if key in self._cache:
self._cache.move_to_end(key)
self._cache[key] = (value, time.time())
if len(self._cache) > self.max_size:
self._cache.popitem(last=False)
def delete(self, key: str) -> bool:
with self._lock:
if key in self._cache:
del self._cache[key]
return True
return False
def clear(self) -> None:
with self._lock:
self._cache.clear()
def stats(self) -> dict:
total = self.hits + self.misses
return {
"size": len(self._cache),
"max_size": self.max_size,
"hits": self.hits,
"misses": self.misses,
"hit_rate": f"{(self.hits / total * 100):.1f}%" if total else "0%",
}
class DiskCache:
"""Persistent disk-based cache with TTL."""
def __init__(self, directory: str = "cache", ttl: int = 3600) -> None:
self.directory = Path(directory)
self.ttl = ttl
self.directory.mkdir(parents=True, exist_ok=True)
def _path(self, key: str) -> Path:
safe = hashlib.md5(key.encode()).hexdigest()
return self.directory / f"{safe}.json"
def get(self, key: str) -> Optional[Any]:
path = self._path(key)
if not path.exists():
return None
try:
data = json.loads(path.read_text(encoding="utf-8"))
if (time.time() - data["ts"]) > self.ttl:
path.unlink(missing_ok=True)
return None
return data["value"]
except Exception as exc:
logger.debug("Disk cache read error: %s", exc)
return None
def set(self, key: str, value: Any) -> None:
path = self._path(key)
try:
path.write_text(
json.dumps({"ts": time.time(), "value": value}, default=str),
encoding="utf-8",
)
except Exception as exc:
logger.debug("Disk cache write error: %s", exc)
def delete(self, key: str) -> bool:
path = self._path(key)
if path.exists():
path.unlink()
return True
return False
def clear(self) -> None:
for f in self.directory.glob("*.json"):
f.unlink(missing_ok=True)
def purge_expired(self) -> int:
removed = 0
for f in self.directory.glob("*.json"):
try:
data = json.loads(f.read_text(encoding="utf-8"))
if (time.time() - data["ts"]) > self.ttl:
f.unlink()
removed += 1
except Exception:
f.unlink(missing_ok=True)
removed += 1
return removed
class AnalysisCache:
"""Two-tier cache (memory + disk) for code analysis results."""
def __init__(self, directory: str = "cache", ttl: int = 3600,
max_memory: int = 500, enabled: bool = True) -> None:
self.enabled = enabled
self.memory = LRUCache(max_size=max_memory, ttl=ttl)
self.disk = DiskCache(directory=directory, ttl=ttl)
@staticmethod
def make_key(code: str, language: str, analysis_level: str = "standard") -> str:
payload = f"{language}:{analysis_level}:{code}"
return hashlib.sha256(payload.encode()).hexdigest()
def get(self, key: str) -> Optional[dict]:
if not self.enabled:
return None
result = self.memory.get(key)
if result is not None:
return result
result = self.disk.get(key)
if result is not None:
self.memory.set(key, result) # Warm up memory cache
return result
def set(self, key: str, value: dict) -> None:
if not self.enabled:
return
self.memory.set(key, value)
self.disk.set(key, value)
def invalidate(self, key: str) -> None:
self.memory.delete(key)
self.disk.delete(key)
def clear_all(self) -> None:
self.memory.clear()
self.disk.clear()
def stats(self) -> dict:
return {"memory": self.memory.stats(), "enabled": self.enabled}
# ─── Module-level singleton ──────────────────────────────────────────────────
_cache_instance: Optional[AnalysisCache] = None
def get_cache() -> AnalysisCache:
global _cache_instance
if _cache_instance is None:
try:
from config import get_config
cfg = get_config()
_cache_instance = AnalysisCache(
directory=cfg.cache.directory,
ttl=cfg.cache.ttl,
max_memory=cfg.cache.max_size,
enabled=cfg.cache.enabled,
)
except Exception:
_cache_instance = AnalysisCache()
return _cache_instance