-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkv_cache.py
More file actions
348 lines (278 loc) · 11.4 KB
/
kv_cache.py
File metadata and controls
348 lines (278 loc) · 11.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python3
"""KV-Cache System for MateCode - Core KV-Cache storage and retrieval
Based on Manus Context Engineering principles:
- Implement true KV-Cache storage (not just structural optimization)
- Cache entire prompts with same static prefix
- Provide basic cache hit rate statistics
- Simple TTL-based cache invalidation
"""
import hashlib
import json
import sqlite3
import time
from datetime import datetime, timedelta
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
from dataclasses import dataclass, asdict
@dataclass
class CacheStats:
"""Cache statistics"""
hit_count: int = 0
miss_count: int = 0
total_queries: int = 0
hit_rate: float = 0.0
cache_size: int = 0 # number of entries
class KVCacheManager:
"""KV-Cache Manager - Core cache storage and retrieval
Design principles:
1. True KV-Cache storage (not just structural optimization)
2. Simple cache key generation (static prefix hash + user ID)
3. Basic TTL-based cache invalidation
4. Minimal statistics tracking
"""
def __init__(self, cache_dir: Optional[Path] = None):
"""Initialize KV-Cache manager
Args:
cache_dir: Directory for cache storage (default: ~/.matecode/kv_cache)
"""
self.cache_dir = cache_dir or Path.home() / ".matecode" / "kv_cache"
self.cache_dir.mkdir(parents=True, exist_ok=True)
self.db_path = self.cache_dir / "kv_cache.db"
self._init_cache_db()
self.stats = CacheStats()
self._update_stats()
def _init_cache_db(self):
"""Initialize cache database"""
with sqlite3.connect(self.db_path) as conn:
# Main cache table
conn.execute("""
CREATE TABLE IF NOT EXISTS kv_cache (
cache_key TEXT PRIMARY KEY,
full_prompt TEXT NOT NULL,
static_prefix_hash TEXT NOT NULL,
user_id TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_accessed TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
access_count INTEGER DEFAULT 0,
ttl_seconds INTEGER DEFAULT 3600 -- 1 hour default TTL
)
""")
# Index for efficient queries
conn.execute("""
CREATE INDEX IF NOT EXISTS idx_cache_user_prefix
ON kv_cache(user_id, static_prefix_hash)
""")
conn.execute("""
CREATE INDEX IF NOT EXISTS idx_cache_last_accessed
ON kv_cache(last_accessed)
""")
conn.commit()
def generate_cache_key(self, static_prefix: str, user_id: str) -> str:
"""Generate cache key based on static prefix hash and user ID
Args:
static_prefix: The static prefix string (STATIC_SYSTEM_PREFIX)
user_id: User/chat identifier
Returns:
Cache key string
"""
# Hash the static prefix (MD5 is sufficient for this purpose)
prefix_hash = hashlib.md5(static_prefix.encode()).hexdigest()[:16]
# Combine with user ID for per-user caching
return f"kv_cache:{user_id}:{prefix_hash}"
def get_cached_prompt(self, cache_key: str) -> Optional[str]:
"""Get cached prompt by cache key
Args:
cache_key: Cache key generated by generate_cache_key()
Returns:
Cached prompt string if found and valid, None otherwise
"""
try:
with sqlite3.connect(self.db_path) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
# Check if cache entry exists and is not expired using SQLite datetime functions
# This avoids timezone issues by doing all datetime math in SQLite
cursor.execute("""
SELECT full_prompt, ttl_seconds, created_at,
(julianday('now') - julianday(created_at)) * 86400 as age_seconds
FROM kv_cache
WHERE cache_key = ?
""", (cache_key,))
row = cursor.fetchone()
if not row:
self.stats.miss_count += 1
self.stats.total_queries += 1
return None
ttl_seconds = row["ttl_seconds"]
age_seconds = row["age_seconds"]
if age_seconds > ttl_seconds:
# Cache expired, delete it
cursor.execute("DELETE FROM kv_cache WHERE cache_key = ?", (cache_key,))
conn.commit()
self.stats.miss_count += 1
self.stats.total_queries += 1
return None
# Update access statistics
cursor.execute("""
UPDATE kv_cache
SET last_accessed = CURRENT_TIMESTAMP,
access_count = access_count + 1
WHERE cache_key = ?
""", (cache_key,))
conn.commit()
self.stats.hit_count += 1
self.stats.total_queries += 1
return row["full_prompt"]
except Exception as e:
print(f"Error retrieving from KV-Cache: {e}")
self.stats.miss_count += 1
self.stats.total_queries += 1
return None
def store_prompt(self, cache_key: str, full_prompt: str,
static_prefix: str, user_id: str,
ttl_seconds: int = 3600) -> bool:
"""Store prompt in cache
Args:
cache_key: Cache key generated by generate_cache_key()
full_prompt: Complete prompt string to cache
static_prefix: Static prefix string (for reference)
user_id: User/chat identifier
ttl_seconds: Time-to-live in seconds (default: 1 hour)
Returns:
True if successful, False otherwise
"""
try:
prefix_hash = hashlib.md5(static_prefix.encode()).hexdigest()[:16]
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
# Insert or replace cache entry
cursor.execute("""
INSERT OR REPLACE INTO kv_cache
(cache_key, full_prompt, static_prefix_hash, user_id, ttl_seconds)
VALUES (?, ?, ?, ?, ?)
""", (cache_key, full_prompt, prefix_hash, user_id, ttl_seconds))
conn.commit()
# Update cache size stat
self._update_stats()
return True
except Exception as e:
print(f"Error storing to KV-Cache: {e}")
return False
def invalidate_cache(self, cache_key: Optional[str] = None,
user_id: Optional[str] = None) -> int:
"""Invalidate cache entries
Args:
cache_key: Specific cache key to invalidate (optional)
user_id: Invalidate all cache entries for a user (optional)
Returns:
Number of cache entries invalidated
"""
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
if cache_key:
cursor.execute("DELETE FROM kv_cache WHERE cache_key = ?", (cache_key,))
elif user_id:
cursor.execute("DELETE FROM kv_cache WHERE user_id = ?", (user_id,))
else:
# Invalidate all expired entries
cursor.execute("""
DELETE FROM kv_cache
WHERE (julianday('now') - julianday(created_at)) * 86400 > ttl_seconds
""")
deleted_count = cursor.rowcount
conn.commit()
self._update_stats()
return deleted_count
except Exception as e:
print(f"Error invalidating KV-Cache: {e}")
return 0
def _update_stats(self):
"""Update cache statistics"""
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM kv_cache")
self.stats.cache_size = cursor.fetchone()[0]
# Calculate hit rate
total = self.stats.total_queries
self.stats.hit_rate = self.stats.hit_count / total if total > 0 else 0.0
except Exception as e:
print(f"Error updating KV-Cache stats: {e}")
def get_stats(self) -> Dict[str, Any]:
"""Get cache statistics
Returns:
Dictionary with cache statistics
"""
self._update_stats()
return {
"hit_count": self.stats.hit_count,
"miss_count": self.stats.miss_count,
"total_queries": self.stats.total_queries,
"hit_rate": self.stats.hit_rate,
"cache_size": self.stats.cache_size,
"cache_dir": str(self.cache_dir),
"db_path": str(self.db_path),
}
def clear_cache(self) -> int:
"""Clear all cache entries
Returns:
Number of cache entries cleared
"""
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM kv_cache")
count = cursor.fetchone()[0]
cursor.execute("DELETE FROM kv_cache")
conn.commit()
self._update_stats()
return count
except Exception as e:
print(f"Error clearing KV-Cache: {e}")
return 0
# Singleton instance pattern (similar to other memory modules)
_kv_cache_instance: Optional[KVCacheManager] = None
def get_kv_cache(cache_dir: Optional[Path] = None) -> KVCacheManager:
"""Get singleton KV-Cache instance
Args:
cache_dir: Directory for cache storage (optional)
Returns:
KV-Cache manager instance
"""
global _kv_cache_instance
if _kv_cache_instance is None:
_kv_cache_instance = KVCacheManager(cache_dir)
return _kv_cache_instance
# Convenience functions
def cache_prompt(static_prefix: str, user_id: str, full_prompt: str,
ttl_seconds: int = 3600) -> bool:
"""Convenience function: cache a prompt
Args:
static_prefix: Static prefix string
user_id: User/chat identifier
full_prompt: Complete prompt string
ttl_seconds: Time-to-live in seconds
Returns:
True if successful
"""
cache = get_kv_cache()
cache_key = cache.generate_cache_key(static_prefix, user_id)
return cache.store_prompt(cache_key, full_prompt, static_prefix, user_id, ttl_seconds)
def get_cached_prompt(static_prefix: str, user_id: str) -> Optional[str]:
"""Convenience function: get cached prompt
Args:
static_prefix: Static prefix string
user_id: User/chat identifier
Returns:
Cached prompt string if found, None otherwise
"""
cache = get_kv_cache()
cache_key = cache.generate_cache_key(static_prefix, user_id)
return cache.get_cached_prompt(cache_key)
def get_cache_stats() -> Dict[str, Any]:
"""Convenience function: get cache statistics
Returns:
Dictionary with cache statistics
"""
return get_kv_cache().get_stats()