File size: 1,117 Bytes
afd56bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import hashlib
import json
from typing import Optional, List, Dict, Any

# Prosta implementacja Semantic/Context Caching w pamięci In-Memory.
# W pełnej skali zintegrowane np. z Redis lub Google Context Caching dla Gemini API.


class ContextCache:
    def __init__(self):
        self._cache = {}

    def _generate_key(self, query: str, filters: Dict[str, Any]) -> str:
        cache_data = {"query": query.lower().strip(), "filters": filters or {}}
        return hashlib.md5(json.dumps(cache_data, sort_keys=True).encode()).hexdigest()

    def get(self, query: str, filters: Dict[str, Any] = None) -> Optional[List[Dict]]:
        key = self._generate_key(query, filters)
        if key in self._cache:
            print("CACHE HIT! Zaoszczędzono czas i koszty zapytań (Context Caching).")
            return self._cache[key]
        return None

    def set(self, query: str, results: List[Dict], filters: Dict[str, Any] = None):
        key = self._generate_key(query, filters)
        self._cache[key] = results


# Globalna instancja systemu (można przepisać na Redisa)
semantic_cache = ContextCache()