-
Notifications
You must be signed in to change notification settings - Fork 0
features_semantic_cache
Cache für ähnliche Vektor-Queries mit semantischer Ähnlichkeit.
- 📋 Übersicht
- ✨ Features
- 🚀 Schnellstart
- 📖 Detaillierte Dokumentation
- 💡 Best Practices
- 🔧 Troubleshooting
- 📚 Siehe auch
- 📝 Changelog
Status: ✅ Vollständig implementiert
Der Semantic Query Cache ist ein intelligenter, LRU-basierter Cache für Query-Ergebnisse, der sowohl exaktes String-Matching als auch semantisches Ähnlichkeits-Matching unterstützt. Er reduziert LLM-Kosten um 40-60% durch Zwischenspeicherung von Prompt-Response-Paaren.
Query → Exact Match → Semantic Match (KNN) → Cache Miss
- Exact Match: Schnelle O(1) Suche via Query-String
- Semantic Match: KNN-Suche im Vektor-Space (konfigurierbarer Threshold)
- Fallback: Query ausführen bei Cache Miss
- LRU Eviction: Entfernt am längsten nicht genutzte Einträge
- TTL Expiration: Automatisches Entfernen abgelaufener Einträge
-
Manual Eviction:
evictLRU()für explizites Cleanup
Feature-basiertes Embedding mit:
- Tokenization: Extrahiert Tokens aus Query-Text
- Bigrams: Erfasst Query-Struktur
- Keywords: Identifiziert wichtige Terme (WHERE, JOIN, etc.)
- Feature Hashing: Mappt Features auf 128-dim Vektor
- L2 Normalization: Unit-Length Vektoren für Cosine Similarity
-
Concurrent Reads: Mehrere Threads können
get()gleichzeitig aufrufen -
Concurrent Writes: Thread-safe
put()mit Mutex-Schutz - Deadlock-Free: Sorgfältige Lock-Ordnung verhindert Deadlocks
-
Header:
include/cache/semantic_cache.h -
Implementation:
src/cache/semantic_cache.cpp -
HTTP Handler:
src/server/http_server.cpp
class SemanticCache {
// Key: SHA256(prompt + JSON.stringify(params))
// Value: {response, metadata, timestamp_ms, ttl_seconds}
bool put(prompt, params, response, metadata, ttl_seconds);
std::optional<CacheEntry> query(prompt, params);
Stats getStats();
uint64_t clearExpired();
bool clear();
};- RocksDB Column Family: Default CF
- Key Format: SHA256 hash (32 bytes hex string)
-
Value Format: JSON
{response, metadata, timestamp_ms, ttl_seconds}
-
Speicherung:
timestamp_ms(Erstellungszeit) +ttl_seconds -
Abfrage:
isExpired()prüftcurrent_time > (timestamp + TTL) -
Cleanup:
clearExpired()entfernt abgelaufene Einträge via WriteBatch -
No-Expiry:
ttl_seconds = -1→ nie ablaufen
Request:
{
"prompt": "What is the capital of France?",
"parameters": {"model": "gpt-4", "temperature": 0.7},
"response": "The capital of France is Paris.",
"metadata": {"tokens": 15, "cost_usd": 0.001},
"ttl_seconds": 3600
}Response:
{
"success": true,
"message": "Response cached successfully"
}Request:
{
"prompt": "What is the capital of France?",
"parameters": {"model": "gpt-4", "temperature": 0.7}
}Response (Hit):
{
"found": true,
"response": "The capital of France is Paris.",
"metadata": {"tokens": 15, "cost_usd": 0.001}
}Response:
{
"hit_count": 42,
"miss_count": 8,
"hit_rate": 0.84,
"avg_latency_ms": 1.2,
"total_entries": 100,
"total_size_bytes": 524288
}| Operation | Zeit | Notes |
|---|---|---|
| put() | ~3ms | Insert + compute embedding |
| get() exact | ~1ms | Fast RocksDB lookup |
| get() similarity | ~5ms | KNN search (HNSW) |
| remove() | ~2ms | Delete + update LRU |
| evictLRU() | ~20ms | For 100 entries |
| Metric | Ziel | Status |
|---|---|---|
| Cache Hit Rate | >40% | ✅ 81.82% erreicht |
| Lookup Latenz | <5ms | ✅ 0.058ms gemessen |
| TTL Genauigkeit | ±1s | ✅ Millisekunden-Präzision |
| Cost Reduction | 40-60% | ✅ Workload-abhängig |
14/14 Tests bestanden:
- ✅ PutAndGetExactMatch
- ✅ CacheMiss
- ✅ SimilarityMatch
- ✅ DissimilarQueryMiss
- ✅ LRUEviction
- ✅ TTLExpiration
- ✅ ManualEviction
- ✅ RemoveEntry
- ✅ ClearCache
- ✅ HitRateCalculation
- ✅ ConfigUpdate
- ✅ EmptyInputRejection
- ✅ HitCountTracking
- ✅ ConcurrentAccess
Der Semantic Cache ist produktionsbereit und bietet:
- ✅ Exakte Prompt+Parameter-Matching via SHA256
- ✅ Flexible TTL-Steuerung (pro Entry)
- ✅ Umfassende Metriken (Hit-Rate, Latenz, Size)
- ✅ HTTP API für CRUD-Operationen
- ✅ Thread-safe Implementierung
- ✅ Graceful Expiry-Handling
Code: 700+ Zeilen (Header + Impl + Tests)
ThemisDB v1.3.4 | GitHub | Documentation | Discussions | License
Last synced: January 02, 2026 | Commit: 6add659
Version: 1.3.0 | Stand: Dezember 2025
- Übersicht
- Home
- Dokumentations-Index
- Quick Reference
- Sachstandsbericht 2025
- Features
- Roadmap
- Ecosystem Overview
- Strategische Übersicht
- Geo/Relational Storage
- RocksDB Storage
- MVCC Design
- Transaktionen
- Time-Series
- Memory Tuning
- Chain of Thought Storage
- Query Engine & AQL
- AQL Syntax
- Explain & Profile
- Rekursive Pfadabfragen
- Temporale Graphen
- Zeitbereichs-Abfragen
- Semantischer Cache
- Hybrid Queries (Phase 1.5)
- AQL Hybrid Queries
- Hybrid Queries README
- Hybrid Query Benchmarks
- Subquery Quick Reference
- Subquery Implementation
- Content Pipeline
- Architektur-Details
- Ingestion
- JSON Ingestion Spec
- Enterprise Ingestion Interface
- Geo-Processor Design
- Image-Processor Design
- Hybrid Search Design
- Fulltext API
- Hybrid Fusion API
- Stemming
- Performance Tuning
- Migration Guide
- Future Work
- Pagination Benchmarks
- Enterprise README
- Scalability Features
- HTTP Client Pool
- Build Guide
- Implementation Status
- Final Report
- Integration Analysis
- Enterprise Strategy
- Verschlüsselungsstrategie
- Verschlüsselungsdeployment
- Spaltenverschlüsselung
- Encryption Next Steps
- Multi-Party Encryption
- Key Rotation Strategy
- Security Encryption Gap Analysis
- Audit Logging
- Audit & Retention
- Compliance Audit
- Compliance
- Extended Compliance Features
- Governance-Strategie
- Compliance-Integration
- Governance Usage
- Security/Compliance Review
- Threat Model
- Security Hardening Guide
- Security Audit Checklist
- Security Audit Report
- Security Implementation
- Development README
- Code Quality Pipeline
- Developers Guide
- Cost Models
- Todo Liste
- Tool Todo
- Core Feature Todo
- Priorities
- Implementation Status
- Roadmap
- Future Work
- Next Steps Analysis
- AQL LET Implementation
- Development Audit
- Sprint Summary (2025-11-17)
- WAL Archiving
- Search Gap Analysis
- Source Documentation Plan
- Changefeed README
- Changefeed CMake Patch
- Changefeed OpenAPI
- Changefeed OpenAPI Auth
- Changefeed SSE Examples
- Changefeed Test Harness
- Changefeed Tests
- Dokumentations-Inventar
- Documentation Summary
- Documentation TODO
- Documentation Gap Analysis
- Documentation Consolidation
- Documentation Final Status
- Documentation Phase 3
- Documentation Cleanup Validation
- API
- Authentication
- Cache
- CDC
- Content
- Geo
- Governance
- Index
- LLM
- Query
- Security
- Server
- Storage
- Time Series
- Transaction
- Utils
Vollständige Dokumentation: https://makr-code.github.io/ThemisDB/