-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcache_persistence_demo.py
More file actions
136 lines (109 loc) · 4.69 KB
/
Copy pathcache_persistence_demo.py
File metadata and controls
136 lines (109 loc) · 4.69 KB
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
#!/usr/bin/env python3
"""
Demo script for semantic cache persistence functionality
Demonstrates cache persistence across application restarts
"""
import sys
import os
import time
from pathlib import Path
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from nexum_ai.optimizer import SemanticCache
def demo_cache_persistence():
"""Demonstrate semantic cache persistence functionality"""
print("NexumDB Semantic Cache Persistence Demo")
print("=" * 60)
# Session 1: Populate cache
print("\nSession 1: Populating semantic cache...")
cache1 = SemanticCache(cache_file="demo_cache.sqlite")
# Simulate typical database queries
demo_queries = [
("SELECT * FROM users WHERE age > 25", "Found 42 users older than 25"),
("SELECT name, email FROM customers WHERE city = 'New York'", "NYC customers: Alice, Bob, Charlie"),
("SELECT COUNT(*) FROM orders WHERE status = 'pending'", "Pending orders: 15"),
("SELECT product_name FROM inventory WHERE stock < 10", "Low stock items: Widget A, Gadget B"),
("SELECT AVG(price) FROM products WHERE category = 'electronics'", "Average electronics price: $299.99")
]
for i, (query, result) in enumerate(demo_queries, 1):
print(f" {i}. Caching: {query[:40]}...")
cache1.put(query, result)
time.sleep(0.1) # Simulate processing time
# Save cache after adding all entries
cache1.save_cache()
stats1 = cache1.get_cache_stats()
print(f"\n📊 Cache populated: {stats1['total_entries']} entries")
print(f" Cache file: {stats1['cache_file']}")
print(f" File size: {stats1['cache_size_bytes']} bytes")
print("\nSimulating application restart...")
print(" (Creating new cache instance)")
del cache1 # Simulate application shutdown
# Session 2: Load from disk
print("\nSession 2: Loading cache from disk...")
cache2 = SemanticCache(cache_file="demo_cache.sqlite")
stats2 = cache2.get_cache_stats()
print(f" Cache loaded: {stats2['total_entries']} entries")
# Testing cache hits
print("\nTesting cache hits after restart...")
hit_count = 0
for i, (query, expected_result) in enumerate(demo_queries, 1):
cached_result = cache2.get(query)
if cached_result:
hit_count += 1
print(f" Hit {i}: {query[:35]}...")
print(f" Result: {cached_result}")
else:
print(f" Miss {i}: {query[:35]}...")
print(f"\n📈 Cache Performance:")
print(f" Cache hits: {hit_count}/{len(demo_queries)}")
print(f" Hit rate: {(hit_count/len(demo_queries)*100):.1f}%")
# Testing semantic similarity
print("\nTesting semantic similarity...")
similar_queries = [
"SELECT * FROM users WHERE age > 25", # Exact match
"SELECT * FROM users WHERE age >= 26", # Similar query
"Show me users older than 25", # Natural language variant
]
for query in similar_queries:
result = cache2.get(query)
if result:
print(f" Similar hit: {query}")
else:
print(f" No match: {query}")
# Export to JSON
print("\nExporting cache to JSON format...")
cache2.save_cache_json("demo_cache.json")
# Cache management
print("\nCache management features:")
print(f" Current threshold: {cache2.similarity_threshold}")
print(" Optimizing cache (keeping last 3 entries)...")
cache2.optimize_cache(max_entries=3)
final_stats = cache2.get_cache_stats()
print(f" Optimized cache: {final_stats['total_entries']} entries")
# Environment variable configuration
print("\nEnvironment variable configuration:")
print(" Set NEXUMDB_CACHE_FILE to customize cache location")
print(" Example: export NEXUMDB_CACHE_FILE=my_custom_cache.sqlite")
# Cleanup
print("\nCleaning up demo files...")
cache2.clear()
# Remove JSON file if it exists
json_file = Path("demo_cache.json")
if json_file.exists():
json_file.unlink()
print(" Removed demo_cache.json")
print("\nDemo completed successfully!")
print("\nKey Benefits:")
print(" - Cache persists across application restarts")
print(" - Configurable cache file location")
print(" - JSON export for debugging/analysis")
print(" - Automatic cache optimization")
print(" - Semantic similarity matching")
if __name__ == "__main__":
try:
demo_cache_persistence()
except KeyboardInterrupt:
print("\n\nWarning: Demo interrupted by user")
except Exception as e:
print(f"\nError: Demo failed: {e}")
sys.exit(1)