Spaces:
Running
Running
File size: 1,686 Bytes
956120a 0d03152 58dc571 0d03152 58dc571 0d03152 58dc571 0d03152 956120a | 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 | """In-memory TTL cache for generation results."""
from __future__ import annotations
import hashlib
import time
from typing import Optional
from src.core.generator import GenerationResult
def cache_key(
query: str,
model: str,
top_k: int,
*,
provider: str = "ollama",
use_rerank: bool = True,
reranker_model: str = "",
corpus_fingerprint: str = "documents",
response_mode: str = "sync",
) -> str:
"""response_mode distinguishes streaming vs non-streaming cache entries (must not collide)."""
raw = "\n".join(
[
query,
provider,
model,
str(top_k),
str(use_rerank),
reranker_model,
corpus_fingerprint,
response_mode,
]
)
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
class ResponseCache:
"""Simple process-local cache with TTL."""
def __init__(self, ttl_seconds: int = 300) -> None:
self.ttl_seconds = ttl_seconds
self._store: dict[str, tuple[GenerationResult, float]] = {}
def get(self, key: str) -> Optional[GenerationResult]:
if self.ttl_seconds <= 0:
return None
item = self._store.get(key)
if not item:
return None
result, expires_at = item
if time.monotonic() > expires_at:
del self._store[key]
return None
return result
def set(self, key: str, result: GenerationResult) -> None:
if self.ttl_seconds <= 0:
return
self._store[key] = (result, time.monotonic() + float(self.ttl_seconds))
def clear(self) -> None:
self._store.clear()
|