askrobots / dbbasic Public

Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.

0 downloads

dbbasic/dbbasic_cache.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add DBBasic crate engine logo and update README 0d628db 1 year, 2 months ago History
dbbasic_cache.py · 18.5 KB · 605 lines
1#!/usr/bin/env python3
2"""
3DBBasic Caching System
4Configuration-driven multi-layer caching for high performance applications
5"""
6
7import json
8import time
9import hashlib
10import pickle
11import logging
12from typing import Dict, Any, Optional, List, Union
13from datetime import datetime, timedelta
14from dataclasses import dataclass
15from enum import Enum
16import asyncio
17import redis
18import sqlite3
19import threading
20from pathlib import Path
21
22logger = logging.getLogger(__name__)
23
24class CacheStrategy(Enum):
25 MEMORY = "memory"
26 REDIS = "redis"
27 SQLITE = "sqlite"
28 MULTI_LAYER = "multi_layer"
29
30class CachePolicy(Enum):
31 LRU = "lru" # Least Recently Used
32 LFU = "lfu" # Least Frequently Used
33 TTL = "ttl" # Time To Live
34 WRITE_THROUGH = "write_through"
35 WRITE_BACK = "write_back"
36
37@dataclass
38class CacheConfig:
39 strategy: CacheStrategy
40 policy: CachePolicy
41 max_size: int = 1000
42 ttl_seconds: int = 3600
43 redis_url: str = "redis://localhost:6379"
44 sqlite_path: str = "cache.db"
45 compression: bool = False
46 serialization: str = "json" # json, pickle
47
48@dataclass
49class CacheEntry:
50 key: str
51 value: Any
52 created_at: datetime
53 last_accessed: datetime
54 access_count: int
55 ttl_seconds: Optional[int]
56 size_bytes: int
57
58class MemoryCache:
59 """In-memory cache with LRU/LFU policies"""
60
61 def __init__(self, config: CacheConfig):
62 self.config = config
63 self.data: Dict[str, CacheEntry] = {}
64 self.lock = threading.RLock()
65
66 def get(self, key: str) -> Optional[Any]:
67 with self.lock:
68 if key not in self.data:
69 return None
70
71 entry = self.data[key]
72
73 # Check TTL
74 if entry.ttl_seconds and self._is_expired(entry):
75 del self.data[key]
76 return None
77
78 # Update access statistics
79 entry.last_accessed = datetime.now()
80 entry.access_count += 1
81
82 return entry.value
83
84 def set(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> bool:
85 with self.lock:
86 # Serialize value to calculate size
87 serialized = self._serialize(value)
88 size_bytes = len(serialized)
89
90 # Check if we need to evict entries
91 if len(self.data) >= self.config.max_size:
92 self._evict()
93
94 entry = CacheEntry(
95 key=key,
96 value=value,
97 created_at=datetime.now(),
98 last_accessed=datetime.now(),
99 access_count=1,
100 ttl_seconds=ttl_seconds or self.config.ttl_seconds,
101 size_bytes=size_bytes
102 )
103
104 self.data[key] = entry
105 return True
106
107 def delete(self, key: str) -> bool:
108 with self.lock:
109 if key in self.data:
110 del self.data[key]
111 return True
112 return False
113
114 def clear(self):
115 with self.lock:
116 self.data.clear()
117
118 def _evict(self):
119 """Evict entries based on cache policy"""
120 if not self.data:
121 return
122
123 if self.config.policy == CachePolicy.LRU:
124 # Remove least recently used
125 oldest_key = min(self.data.keys(),
126 key=lambda k: self.data[k].last_accessed)
127 del self.data[oldest_key]
128
129 elif self.config.policy == CachePolicy.LFU:
130 # Remove least frequently used
131 least_used_key = min(self.data.keys(),
132 key=lambda k: self.data[k].access_count)
133 del self.data[least_used_key]
134
135 elif self.config.policy == CachePolicy.TTL:
136 # Remove expired entries first
137 now = datetime.now()
138 expired_keys = [
139 key for key, entry in self.data.items()
140 if entry.ttl_seconds and (now - entry.created_at).seconds > entry.ttl_seconds
141 ]
142 for key in expired_keys:
143 del self.data[key]
144
145 # If still over capacity, fall back to LRU
146 if len(self.data) >= self.config.max_size:
147 self._evict_lru()
148
149 def _is_expired(self, entry: CacheEntry) -> bool:
150 if not entry.ttl_seconds:
151 return False
152 age = (datetime.now() - entry.created_at).seconds
153 return age > entry.ttl_seconds
154
155 def _serialize(self, value: Any) -> bytes:
156 if self.config.serialization == "pickle":
157 return pickle.dumps(value)
158 else:
159 return json.dumps(value, default=str).encode('utf-8')
160
161class RedisCache:
162 """Redis-based distributed cache"""
163
164 def __init__(self, config: CacheConfig):
165 self.config = config
166 try:
167 import redis
168 self.client = redis.from_url(config.redis_url)
169 self.client.ping() # Test connection
170 except Exception as e:
171 logger.error(f"Redis connection failed: {e}")
172 self.client = None
173
174 def get(self, key: str) -> Optional[Any]:
175 if not self.client:
176 return None
177
178 try:
179 data = self.client.get(key)
180 if data is None:
181 return None
182
183 return self._deserialize(data)
184 except Exception as e:
185 logger.error(f"Redis get failed: {e}")
186 return None
187
188 def set(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> bool:
189 if not self.client:
190 return False
191
192 try:
193 serialized = self._serialize(value)
194 ttl = ttl_seconds or self.config.ttl_seconds
195
196 if ttl:
197 return self.client.setex(key, ttl, serialized)
198 else:
199 return self.client.set(key, serialized)
200 except Exception as e:
201 logger.error(f"Redis set failed: {e}")
202 return False
203
204 def delete(self, key: str) -> bool:
205 if not self.client:
206 return False
207
208 try:
209 return bool(self.client.delete(key))
210 except Exception as e:
211 logger.error(f"Redis delete failed: {e}")
212 return False
213
214 def clear(self):
215 if self.client:
216 try:
217 self.client.flushdb()
218 except Exception as e:
219 logger.error(f"Redis clear failed: {e}")
220
221 def _serialize(self, value: Any) -> bytes:
222 if self.config.serialization == "pickle":
223 return pickle.dumps(value)
224 else:
225 return json.dumps(value, default=str).encode('utf-8')
226
227 def _deserialize(self, data: bytes) -> Any:
228 if self.config.serialization == "pickle":
229 return pickle.loads(data)
230 else:
231 return json.loads(data.decode('utf-8'))
232
233class SqliteCache:
234 """SQLite-based persistent cache"""
235
236 def __init__(self, config: CacheConfig):
237 self.config = config
238 self.db_path = config.sqlite_path
239 self._init_database()
240
241 def _init_database(self):
242 conn = sqlite3.connect(self.db_path)
243 conn.execute('''
244 CREATE TABLE IF NOT EXISTS cache_entries (
245 key TEXT PRIMARY KEY,
246 value BLOB,
247 created_at TIMESTAMP,
248 last_accessed TIMESTAMP,
249 access_count INTEGER,
250 ttl_seconds INTEGER,
251 size_bytes INTEGER
252 )
253 ''')
254 conn.commit()
255 conn.close()
256
257 def get(self, key: str) -> Optional[Any]:
258 conn = sqlite3.connect(self.db_path)
259 cursor = conn.cursor()
260
261 try:
262 cursor.execute(
263 'SELECT value, created_at, ttl_seconds, access_count FROM cache_entries WHERE key = ?',
264 (key,)
265 )
266 row = cursor.fetchone()
267
268 if not row:
269 return None
270
271 value_data, created_at_str, ttl_seconds, access_count = row
272 created_at = datetime.fromisoformat(created_at_str)
273
274 # Check TTL
275 if ttl_seconds and (datetime.now() - created_at).seconds > ttl_seconds:
276 cursor.execute('DELETE FROM cache_entries WHERE key = ?', (key,))
277 conn.commit()
278 return None
279
280 # Update access statistics
281 cursor.execute(
282 'UPDATE cache_entries SET last_accessed = ?, access_count = ? WHERE key = ?',
283 (datetime.now().isoformat(), access_count + 1, key)
284 )
285 conn.commit()
286
287 return self._deserialize(value_data)
288
289 except Exception as e:
290 logger.error(f"SQLite get failed: {e}")
291 return None
292 finally:
293 conn.close()
294
295 def set(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> bool:
296 conn = sqlite3.connect(self.db_path)
297 cursor = conn.cursor()
298
299 try:
300 serialized = self._serialize(value)
301 size_bytes = len(serialized)
302 now = datetime.now().isoformat()
303 ttl = ttl_seconds or self.config.ttl_seconds
304
305 cursor.execute('''
306 INSERT OR REPLACE INTO cache_entries
307 (key, value, created_at, last_accessed, access_count, ttl_seconds, size_bytes)
308 VALUES (?, ?, ?, ?, ?, ?, ?)
309 ''', (key, serialized, now, now, 1, ttl, size_bytes))
310
311 conn.commit()
312 return True
313
314 except Exception as e:
315 logger.error(f"SQLite set failed: {e}")
316 return False
317 finally:
318 conn.close()
319
320 def delete(self, key: str) -> bool:
321 conn = sqlite3.connect(self.db_path)
322 cursor = conn.cursor()
323
324 try:
325 cursor.execute('DELETE FROM cache_entries WHERE key = ?', (key,))
326 conn.commit()
327 return cursor.rowcount > 0
328 except Exception as e:
329 logger.error(f"SQLite delete failed: {e}")
330 return False
331 finally:
332 conn.close()
333
334 def clear(self):
335 conn = sqlite3.connect(self.db_path)
336 try:
337 conn.execute('DELETE FROM cache_entries')
338 conn.commit()
339 except Exception as e:
340 logger.error(f"SQLite clear failed: {e}")
341 finally:
342 conn.close()
343
344 def _serialize(self, value: Any) -> bytes:
345 if self.config.serialization == "pickle":
346 return pickle.dumps(value)
347 else:
348 return json.dumps(value, default=str).encode('utf-8')
349
350 def _deserialize(self, data: bytes) -> Any:
351 if self.config.serialization == "pickle":
352 return pickle.loads(data)
353 else:
354 return json.loads(data.decode('utf-8'))
355
356class MultiLayerCache:
357 """Multi-layer cache combining memory, Redis, and SQLite"""
358
359 def __init__(self, config: CacheConfig):
360 self.config = config
361
362 # L1: Memory cache (fastest)
363 memory_config = CacheConfig(
364 strategy=CacheStrategy.MEMORY,
365 policy=config.policy,
366 max_size=min(100, config.max_size // 10), # 10% of total size
367 ttl_seconds=config.ttl_seconds
368 )
369 self.memory_cache = MemoryCache(memory_config)
370
371 # L2: Redis cache (distributed)
372 self.redis_cache = RedisCache(config)
373
374 # L3: SQLite cache (persistent)
375 self.sqlite_cache = SqliteCache(config)
376
377 def get(self, key: str) -> Optional[Any]:
378 # Try L1 (memory) first
379 value = self.memory_cache.get(key)
380 if value is not None:
381 return value
382
383 # Try L2 (Redis)
384 value = self.redis_cache.get(key)
385 if value is not None:
386 # Promote to L1
387 self.memory_cache.set(key, value)
388 return value
389
390 # Try L3 (SQLite)
391 value = self.sqlite_cache.get(key)
392 if value is not None:
393 # Promote to L1 and L2
394 self.memory_cache.set(key, value)
395 self.redis_cache.set(key, value)
396 return value
397
398 return None
399
400 def set(self, key: str, value: Any, ttl_seconds: Optional[int] = None) -> bool:
401 # Write to all layers
402 results = [
403 self.memory_cache.set(key, value, ttl_seconds),
404 self.redis_cache.set(key, value, ttl_seconds),
405 self.sqlite_cache.set(key, value, ttl_seconds)
406 ]
407 return any(results) # Success if any layer succeeds
408
409 def delete(self, key: str) -> bool:
410 # Delete from all layers
411 results = [
412 self.memory_cache.delete(key),
413 self.redis_cache.delete(key),
414 self.sqlite_cache.delete(key)
415 ]
416 return any(results)
417
418 def clear(self):
419 self.memory_cache.clear()
420 self.redis_cache.clear()
421 self.sqlite_cache.clear()
422
423class CacheManager:
424 """Main cache manager with configuration-driven setup"""
425
426 def __init__(self):
427 self.caches: Dict[str, Union[MemoryCache, RedisCache, SqliteCache, MultiLayerCache]] = {}
428 self.default_config = CacheConfig(
429 strategy=CacheStrategy.MEMORY,
430 policy=CachePolicy.LRU,
431 max_size=1000,
432 ttl_seconds=3600
433 )
434
435 def configure_cache(self, name: str, config: CacheConfig):
436 """Configure a named cache instance"""
437 if config.strategy == CacheStrategy.MEMORY:
438 self.caches[name] = MemoryCache(config)
439 elif config.strategy == CacheStrategy.REDIS:
440 self.caches[name] = RedisCache(config)
441 elif config.strategy == CacheStrategy.SQLITE:
442 self.caches[name] = SqliteCache(config)
443 elif config.strategy == CacheStrategy.MULTI_LAYER:
444 self.caches[name] = MultiLayerCache(config)
445
446 logger.info(f"🗄️ Configured cache '{name}' with strategy {config.strategy.value}")
447
448 def get_cache(self, name: str = "default"):
449 """Get a cache instance by name"""
450 if name not in self.caches:
451 self.configure_cache(name, self.default_config)
452 return self.caches[name]
453
454 def cache_key(self, prefix: str, *args, **kwargs) -> str:
455 """Generate a cache key from prefix and arguments"""
456 # Create deterministic key from arguments
457 key_parts = [prefix]
458
459 # Add positional arguments
460 for arg in args:
461 if isinstance(arg, (dict, list)):
462 key_parts.append(json.dumps(arg, sort_keys=True))
463 else:
464 key_parts.append(str(arg))
465
466 # Add keyword arguments (sorted for consistency)
467 for k, v in sorted(kwargs.items()):
468 if isinstance(v, (dict, list)):
469 key_parts.append(f"{k}:{json.dumps(v, sort_keys=True)}")
470 else:
471 key_parts.append(f"{k}:{v}")
472
473 # Create hash to ensure key length limits
474 key_string = ":".join(key_parts)
475 if len(key_string) > 200: # Most cache systems have key length limits
476 key_hash = hashlib.sha256(key_string.encode()).hexdigest()[:16]
477 return f"{prefix}:{key_hash}"
478
479 return key_string
480
481# Global cache manager instance
482cache_manager = CacheManager()
483
484def get_cache_manager() -> CacheManager:
485 """Get global cache manager instance"""
486 return cache_manager
487
488# Decorators for easy caching
489def cached(cache_name: str = "default", ttl_seconds: Optional[int] = None,
490 key_prefix: str = "func"):
491 """Decorator to cache function results"""
492 def decorator(func):
493 def wrapper(*args, **kwargs):
494 manager = get_cache_manager()
495 cache = manager.get_cache(cache_name)
496
497 # Generate cache key
498 key = manager.cache_key(f"{key_prefix}:{func.__name__}", *args, **kwargs)
499
500 # Try to get from cache
501 result = cache.get(key)
502 if result is not None:
503 return result
504
505 # Call function and cache result
506 result = func(*args, **kwargs)
507 cache.set(key, result, ttl_seconds)
508 return result
509
510 return wrapper
511 return decorator
512
513def cached_async(cache_name: str = "default", ttl_seconds: Optional[int] = None,
514 key_prefix: str = "async_func"):
515 """Decorator to cache async function results"""
516 def decorator(func):
517 async def wrapper(*args, **kwargs):
518 manager = get_cache_manager()
519 cache = manager.get_cache(cache_name)
520
521 # Generate cache key
522 key = manager.cache_key(f"{key_prefix}:{func.__name__}", *args, **kwargs)
523
524 # Try to get from cache
525 result = cache.get(key)
526 if result is not None:
527 return result
528
529 # Call function and cache result
530 result = await func(*args, **kwargs)
531 cache.set(key, result, ttl_seconds)
532 return result
533
534 return wrapper
535 return decorator
536
537# Example configuration for different cache types
538CACHE_CONFIGURATIONS = {
539 "crud_queries": CacheConfig(
540 strategy=CacheStrategy.MULTI_LAYER,
541 policy=CachePolicy.LRU,
542 max_size=5000,
543 ttl_seconds=1800, # 30 minutes
544 redis_url="redis://localhost:6379/1"
545 ),
546 "user_sessions": CacheConfig(
547 strategy=CacheStrategy.REDIS,
548 policy=CachePolicy.TTL,
549 ttl_seconds=3600, # 1 hour
550 redis_url="redis://localhost:6379/2"
551 ),
552 "api_responses": CacheConfig(
553 strategy=CacheStrategy.MEMORY,
554 policy=CachePolicy.LRU,
555 max_size=1000,
556 ttl_seconds=300, # 5 minutes
557 ),
558 "analytics": CacheConfig(
559 strategy=CacheStrategy.SQLITE,
560 policy=CachePolicy.TTL,
561 ttl_seconds=86400, # 24 hours
562 sqlite_path="analytics_cache.db"
563 )
564}
565
566if __name__ == "__main__":
567 # Example usage
568 manager = get_cache_manager()
569
570 # Configure different cache types
571 for name, config in CACHE_CONFIGURATIONS.items():
572 manager.configure_cache(name, config)
573
574 # Test caching
575 cache = manager.get_cache("crud_queries")
576
577 # Set some test data
578 cache.set("user:123", {"name": "John", "email": "john@example.com"})
579 cache.set("product:456", {"name": "Widget", "price": 19.99})
580
581 # Retrieve data
582 user = cache.get("user:123")
583 product = cache.get("product:456")
584
585 print(f"User: {user}")
586 print(f"Product: {product}")
587
588 # Test decorator
589 @cached(cache_name="api_responses", ttl_seconds=60)
590 def expensive_calculation(n):
591 time.sleep(1) # Simulate expensive operation
592 return n * n
593
594 # First call will be slow
595 start = time.time()
596 result1 = expensive_calculation(10)
597 elapsed1 = time.time() - start
598
599 # Second call will be fast (cached)
600 start = time.time()
601 result2 = expensive_calculation(10)
602 elapsed2 = time.time() - start
603
604 print(f"First call: {result1} (took {elapsed1:.2f}s)")
605 print(f"Second call: {result2} (took {elapsed2:.2f}s)")