File size: 8,130 Bytes
a809248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Caching Module for RAG System.
Implements Redis-based caching for embeddings and responses.
"""

import hashlib
import json
import pickle
from typing import Any, Optional, List, Union
from datetime import timedelta

from ..utils import get_logger

logger = get_logger(__name__)


class CacheBackend:
    """Base cache backend interface."""
    
    def get(self, key: str) -> Optional[Any]:
        raise NotImplementedError
    
    def set(self, key: str, value: Any, ttl: Optional[int] = None) -> bool:
        raise NotImplementedError
    
    def delete(self, key: str) -> bool:
        raise NotImplementedError
    
    def exists(self, key: str) -> bool:
        raise NotImplementedError
    
    def clear(self) -> bool:
        raise NotImplementedError


class InMemoryCache(CacheBackend):
    """
    Simple in-memory cache for development.
    """
    
    def __init__(self, max_size: int = 1000):
        """
        Initialize in-memory cache.
        
        Args:
            max_size: Maximum number of entries
        """
        self.max_size = max_size
        self._cache = {}
        self._expiry = {}
        logger.info("In-memory cache initialized")
    
    def get(self, key: str) -> Optional[Any]:
        """Get value from cache."""
        import time
        
        if key in self._expiry:
            if time.time() > self._expiry[key]:
                self.delete(key)
                return None
        
        return self._cache.get(key)
    
    def set(self, key: str, value: Any, ttl: Optional[int] = None) -> bool:
        """Set value in cache."""
        import time
        
        # Evict if at max size
        if len(self._cache) >= self.max_size:
            oldest = next(iter(self._cache))
            self.delete(oldest)
        
        self._cache[key] = value
        if ttl:
            self._expiry[key] = time.time() + ttl
        
        return True
    
    def delete(self, key: str) -> bool:
        """Delete key from cache."""
        self._cache.pop(key, None)
        self._expiry.pop(key, None)
        return True
    
    def exists(self, key: str) -> bool:
        """Check if key exists."""
        return key in self._cache
    
    def clear(self) -> bool:
        """Clear all cache entries."""
        self._cache.clear()
        self._expiry.clear()
        return True


class RedisCache(CacheBackend):
    """
    Redis-based cache for production.
    """
    
    def __init__(
        self,
        host: str = "localhost",
        port: int = 6379,
        db: int = 0,
        password: Optional[str] = None,
        prefix: str = "rag:"
    ):
        """
        Initialize Redis cache.
        
        Args:
            host: Redis host
            port: Redis port
            db: Redis database number
            password: Redis password
            prefix: Key prefix
        """
        self.prefix = prefix
        self._redis = None
        
        try:
            import redis
            self._redis = redis.Redis(
                host=host,
                port=port,
                db=db,
                password=password,
                decode_responses=False
            )
            self._redis.ping()
            logger.info(f"Redis cache connected: {host}:{port}")
        except Exception as e:
            logger.warning(f"Redis not available: {e}")
            self._redis = None
    
    def _key(self, key: str) -> str:
        """Add prefix to key."""
        return f"{self.prefix}{key}"
    
    def get(self, key: str) -> Optional[Any]:
        """Get value from Redis."""
        if not self._redis:
            return None
        
        try:
            data = self._redis.get(self._key(key))
            if data:
                return pickle.loads(data)
            return None
        except Exception as e:
            logger.debug(f"Cache get error: {e}")
            return None
    
    def set(self, key: str, value: Any, ttl: Optional[int] = None) -> bool:
        """Set value in Redis."""
        if not self._redis:
            return False
        
        try:
            data = pickle.dumps(value)
            if ttl:
                self._redis.setex(self._key(key), ttl, data)
            else:
                self._redis.set(self._key(key), data)
            return True
        except Exception as e:
            logger.debug(f"Cache set error: {e}")
            return False
    
    def delete(self, key: str) -> bool:
        """Delete key from Redis."""
        if not self._redis:
            return False
        
        try:
            self._redis.delete(self._key(key))
            return True
        except Exception as e:
            logger.debug(f"Cache delete error: {e}")
            return False
    
    def exists(self, key: str) -> bool:
        """Check if key exists in Redis."""
        if not self._redis:
            return False
        
        try:
            return self._redis.exists(self._key(key)) > 0
        except Exception:
            return False
    
    def clear(self) -> bool:
        """Clear all cache entries with prefix."""
        if not self._redis:
            return False
        
        try:
            keys = self._redis.keys(f"{self.prefix}*")
            if keys:
                self._redis.delete(*keys)
            return True
        except Exception as e:
            logger.debug(f"Cache clear error: {e}")
            return False


class RAGCache:
    """
    High-level caching for RAG operations.
    Caches embeddings, search results, and LLM responses.
    """
    
    def __init__(self, backend: Optional[CacheBackend] = None):
        """
        Initialize RAG cache.
        
        Args:
            backend: Cache backend to use
        """
        if backend:
            self.backend = backend
        else:
            # Try Redis first, fallback to in-memory
            redis_cache = RedisCache()
            if redis_cache._redis:
                self.backend = redis_cache
            else:
                self.backend = InMemoryCache()
        
        self.embedding_ttl = 86400  # 24 hours
        self.search_ttl = 3600  # 1 hour
        self.response_ttl = 1800  # 30 minutes
    
    def _hash_key(self, *args) -> str:
        """Generate cache key from arguments."""
        content = json.dumps(args, sort_keys=True, default=str)
        return hashlib.md5(content.encode()).hexdigest()
    
    def get_embedding(self, text: str, model: str) -> Optional[List[float]]:
        """Get cached embedding."""
        key = f"emb:{self._hash_key(text, model)}"
        return self.backend.get(key)
    
    def set_embedding(self, text: str, model: str, embedding: List[float]) -> bool:
        """Cache an embedding."""
        key = f"emb:{self._hash_key(text, model)}"
        return self.backend.set(key, embedding, self.embedding_ttl)
    
    def get_search_results(self, query: str, top_k: int) -> Optional[Any]:
        """Get cached search results."""
        key = f"search:{self._hash_key(query, top_k)}"
        return self.backend.get(key)
    
    def set_search_results(self, query: str, top_k: int, results: Any) -> bool:
        """Cache search results."""
        key = f"search:{self._hash_key(query, top_k)}"
        return self.backend.set(key, results, self.search_ttl)
    
    def get_response(self, query: str, context_hash: str) -> Optional[str]:
        """Get cached LLM response."""
        key = f"resp:{self._hash_key(query, context_hash)}"
        return self.backend.get(key)
    
    def set_response(self, query: str, context_hash: str, response: str) -> bool:
        """Cache LLM response."""
        key = f"resp:{self._hash_key(query, context_hash)}"
        return self.backend.set(key, response, self.response_ttl)
    
    def invalidate_all(self) -> bool:
        """Invalidate all cached data."""
        return self.backend.clear()


# Global instance
_cache: Optional[RAGCache] = None


def get_cache() -> RAGCache:
    """Get global cache instance."""
    global _cache
    if _cache is None:
        _cache = RAGCache()
    return _cache