forked from arthurcolle/claude-code-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
372 lines (307 loc) · 11.1 KB
/
Copy pathcache.py
File metadata and controls
372 lines (307 loc) · 11.1 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""Caching layer for repeated queries."""
import asyncio
import hashlib
import json
import time
from dataclasses import dataclass, field
from typing import Any, Optional, Union
from collections.abc import AsyncIterator
from pathlib import Path
import pickle
from datetime import datetime, timedelta
from .types import Message, ClaudeCodeOptions
@dataclass
class CacheEntry:
"""A single cache entry."""
key: str
messages: list[Message]
created_at: float = field(default_factory=time.time)
accessed_at: float = field(default_factory=time.time)
access_count: int = 1
ttl_seconds: Optional[float] = None
def is_expired(self) -> bool:
"""Check if entry is expired."""
if self.ttl_seconds is None:
return False
return time.time() - self.created_at > self.ttl_seconds
def touch(self) -> None:
"""Update access time and count."""
self.accessed_at = time.time()
self.access_count += 1
class CacheBackend:
"""Base class for cache backends."""
async def get(self, key: str) -> Optional[CacheEntry]:
"""Get entry from cache."""
raise NotImplementedError
async def set(self, key: str, entry: CacheEntry) -> None:
"""Set entry in cache."""
raise NotImplementedError
async def delete(self, key: str) -> None:
"""Delete entry from cache."""
raise NotImplementedError
async def clear(self) -> None:
"""Clear all entries."""
raise NotImplementedError
async def get_stats(self) -> dict[str, Any]:
"""Get cache statistics."""
raise NotImplementedError
class MemoryCacheBackend(CacheBackend):
"""In-memory cache backend."""
def __init__(self, max_size: int = 1000):
self.max_size = max_size
self._cache: dict[str, CacheEntry] = {}
self._lock = asyncio.Lock()
self._hits = 0
self._misses = 0
async def get(self, key: str) -> Optional[CacheEntry]:
"""Get entry from cache."""
async with self._lock:
entry = self._cache.get(key)
if entry:
if entry.is_expired():
del self._cache[key]
self._misses += 1
return None
entry.touch()
self._hits += 1
return entry
self._misses += 1
return None
async def set(self, key: str, entry: CacheEntry) -> None:
"""Set entry in cache."""
async with self._lock:
# Evict old entries if needed
if len(self._cache) >= self.max_size:
# Evict least recently accessed
lru_key = min(
self._cache.keys(),
key=lambda k: self._cache[k].accessed_at
)
del self._cache[lru_key]
self._cache[key] = entry
async def delete(self, key: str) -> None:
"""Delete entry from cache."""
async with self._lock:
self._cache.pop(key, None)
async def clear(self) -> None:
"""Clear all entries."""
async with self._lock:
self._cache.clear()
self._hits = 0
self._misses = 0
async def get_stats(self) -> dict[str, Any]:
"""Get cache statistics."""
async with self._lock:
total = self._hits + self._misses
hit_rate = self._hits / total if total > 0 else 0
return {
"entries": len(self._cache),
"max_size": self.max_size,
"hits": self._hits,
"misses": self._misses,
"hit_rate": hit_rate,
}
class FileCacheBackend(CacheBackend):
"""File-based cache backend."""
def __init__(self, cache_dir: Union[str, Path] = ".claude_cache"):
self.cache_dir = Path(cache_dir)
self.cache_dir.mkdir(exist_ok=True)
self._stats_file = self.cache_dir / "stats.json"
self._load_stats()
def _load_stats(self) -> None:
"""Load statistics."""
if self._stats_file.exists():
with open(self._stats_file, "r") as f:
self._stats = json.load(f)
else:
self._stats = {"hits": 0, "misses": 0}
def _save_stats(self) -> None:
"""Save statistics."""
with open(self._stats_file, "w") as f:
json.dump(self._stats, f)
def _get_path(self, key: str) -> Path:
"""Get file path for key."""
return self.cache_dir / f"{key}.pkl"
async def get(self, key: str) -> Optional[CacheEntry]:
"""Get entry from cache."""
path = self._get_path(key)
if path.exists():
try:
with open(path, "rb") as f:
entry = pickle.load(f)
if entry.is_expired():
path.unlink()
self._stats["misses"] += 1
self._save_stats()
return None
entry.touch()
# Save updated entry
with open(path, "wb") as f:
pickle.dump(entry, f)
self._stats["hits"] += 1
self._save_stats()
return entry
except Exception:
# Corrupted cache file
path.unlink()
self._stats["misses"] += 1
self._save_stats()
return None
async def set(self, key: str, entry: CacheEntry) -> None:
"""Set entry in cache."""
path = self._get_path(key)
with open(path, "wb") as f:
pickle.dump(entry, f)
async def delete(self, key: str) -> None:
"""Delete entry from cache."""
path = self._get_path(key)
if path.exists():
path.unlink()
async def clear(self) -> None:
"""Clear all entries."""
for path in self.cache_dir.glob("*.pkl"):
path.unlink()
self._stats = {"hits": 0, "misses": 0}
self._save_stats()
async def get_stats(self) -> dict[str, Any]:
"""Get cache statistics."""
entries = len(list(self.cache_dir.glob("*.pkl")))
total = self._stats["hits"] + self._stats["misses"]
hit_rate = self._stats["hits"] / total if total > 0 else 0
return {
"entries": entries,
"hits": self._stats["hits"],
"misses": self._stats["misses"],
"hit_rate": hit_rate,
}
@dataclass
class CacheConfig:
"""Cache configuration."""
backend: CacheBackend = field(default_factory=MemoryCacheBackend)
ttl_seconds: Optional[float] = 3600 # 1 hour default
key_prefix: str = "claude_"
include_options: bool = True
hash_prompt: bool = True
class QueryCache:
"""Cache for Claude queries."""
def __init__(self, config: Optional[CacheConfig] = None):
self.config = config or CacheConfig()
def _generate_key(self, prompt: str, options: Optional[ClaudeCodeOptions]) -> str:
"""Generate cache key."""
# Create key components
components = [self.config.key_prefix]
if self.config.hash_prompt:
# Hash the prompt for shorter keys
prompt_hash = hashlib.sha256(prompt.encode()).hexdigest()[:16]
components.append(prompt_hash)
else:
# Use sanitized prompt
safe_prompt = "".join(c for c in prompt[:50] if c.isalnum() or c in " -_")
components.append(safe_prompt)
if self.config.include_options and options:
# Include relevant options in key
opt_dict = {
"model": options.model,
"system_prompt": options.system_prompt,
"max_thinking_tokens": options.max_thinking_tokens,
}
opt_str = json.dumps(opt_dict, sort_keys=True)
opt_hash = hashlib.sha256(opt_str.encode()).hexdigest()[:8]
components.append(opt_hash)
return "_".join(components)
async def get(
self,
prompt: str,
options: Optional[ClaudeCodeOptions]
) -> Optional[list[Message]]:
"""Get cached messages if available."""
key = self._generate_key(prompt, options)
entry = await self.config.backend.get(key)
if entry:
return entry.messages
return None
async def set(
self,
prompt: str,
options: Optional[ClaudeCodeOptions],
messages: list[Message]
) -> None:
"""Cache messages."""
key = self._generate_key(prompt, options)
entry = CacheEntry(
key=key,
messages=messages,
ttl_seconds=self.config.ttl_seconds
)
await self.config.backend.set(key, entry)
async def invalidate(
self,
prompt: str,
options: Optional[ClaudeCodeOptions]
) -> None:
"""Invalidate cached entry."""
key = self._generate_key(prompt, options)
await self.config.backend.delete(key)
async def clear(self) -> None:
"""Clear all cache entries."""
await self.config.backend.clear()
async def get_stats(self) -> dict[str, Any]:
"""Get cache statistics."""
return await self.config.backend.get_stats()
# Global cache instance
_global_cache: Optional[QueryCache] = None
def get_global_cache(config: Optional[CacheConfig] = None) -> QueryCache:
"""Get or create global cache."""
global _global_cache
if _global_cache is None:
_global_cache = QueryCache(config)
return _global_cache
async def cached_query(
*,
prompt: str,
options: ClaudeCodeOptions | None = None,
cache: Optional[QueryCache] = None,
force_refresh: bool = False
) -> AsyncIterator[Message]:
"""
Query Claude with caching.
Args:
prompt: The prompt to send
options: Query options
cache: Cache instance (uses global if None)
force_refresh: Force cache refresh
Yields:
Messages from conversation (cached or fresh)
Example:
```python
# First query hits Claude
async for msg in cached_query(prompt="Hello"):
print(msg)
# Second identical query uses cache
async for msg in cached_query(prompt="Hello"):
print(msg) # Much faster!
# Force refresh
async for msg in cached_query(
prompt="Hello",
force_refresh=True
):
print(msg)
```
"""
from . import query
if cache is None:
cache = get_global_cache()
# Check cache unless forcing refresh
if not force_refresh:
cached_messages = await cache.get(prompt, options)
if cached_messages:
for message in cached_messages:
yield message
return
# Execute query and collect messages
messages = []
async for message in query(prompt=prompt, options=options):
messages.append(message)
yield message
# Cache the results
await cache.set(prompt, options, messages)