File size: 2,554 Bytes
2379054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
CACHE MANAGER - Query Caching for Speed
"""

import json
import hashlib
from pathlib import Path
from datetime import datetime, timedelta
import threading

class CacheManager:
    def __init__(self, cache_dir=".cache"):
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(exist_ok=True)
        self.cache = {}
        self.lock = threading.Lock()
        self._load_cache()
    
    def _get_cache_key(self, question: str, top_k: int = None) -> str:
        key_string = question.lower().strip()
        if top_k:
            key_string += f"_k{top_k}"
        return hashlib.md5(key_string.encode()).hexdigest()
    
    def _load_cache(self):
        cache_file = self.cache_dir / "cache.json"
        if cache_file.exists():
            try:
                with open(cache_file, 'r') as f:
                    self.cache = json.load(f)
            except Exception as e:
                self.cache = {}
    
    def _save_cache(self):
        cache_file = self.cache_dir / "cache.json"
        try:
            now = datetime.now()
            self.cache = {
                k: v for k, v in self.cache.items()
                if datetime.fromisoformat(v.get('timestamp', '2000-01-01')) > now - timedelta(days=7)
            }
            with open(cache_file, 'w') as f:
                json.dump(self.cache, f)
        except Exception as e:
            pass
    
    def get(self, question: str, top_k: int = None):
        key = self._get_cache_key(question, top_k)
        with self.lock:
            if key in self.cache:
                entry = self.cache[key]
                try:
                    timestamp = datetime.fromisoformat(entry.get('timestamp', '2000-01-01'))
                    if datetime.now() - timestamp < timedelta(days=7):
                        return entry.get('data')
                except Exception as e:
                    pass
        return None
    
    def set(self, question: str, data, top_k: int = None):
        key = self._get_cache_key(question, top_k)
        with self.lock:
            self.cache[key] = {
                'data': data,
                'timestamp': datetime.now().isoformat(),
                'question': question
            }
            self._save_cache()
    
    def clear(self):
        with self.lock:
            self.cache = {}
            self._save_cache()
    
    def stats(self) -> dict:
        return {
            'total_entries': len(self.cache),
            'cache_size_kb': len(str(self.cache)) // 1024
        }


_cache = CacheManager()