File size: 1,414 Bytes
3392ab1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import time
import threading

class CacheManager:
    _instance = None
    _lock = threading.Lock()

    def __new__(cls, max_cache_size=100):
        if cls._instance is None:
            with cls._lock:
                if cls._instance is None:
                    cls._instance = super(CacheManager, cls).__new__(cls)
                    cls._instance.cache = {}
                    cls._instance.max_cache_size = max_cache_size
        return cls._instance

    def normalize_key(self, key):
        return key.strip().lower()

    def add_to_cache(self, key, value, embedding=None):
        normalized_key = self.normalize_key(key)
        if len(self.cache) >= self.max_cache_size:
            self.evict_cache()
        self.cache[normalized_key] = {
            "response": value,
            "timestamp": time.time(),
            "embedding": embedding
        }

    def get_from_cache(self, key):
        normalized_key = self.normalize_key(key)
        return self.cache.get(normalized_key, {}).get("response", None)

    def get_embedding(self, key):
        normalized_key = self.normalize_key(key)
        return self.cache.get(normalized_key, {}).get("embedding", None)

    def evict_cache(self):
        if self.cache:
            oldest_key = min(self.cache, key=lambda k: self.cache[k]["timestamp"])
            del self.cache[oldest_key]

    def clear_cache(self):
        self.cache.clear()