Documentation
¶
Overview ¶
Package cache provides intelligent caching for query results, embeddings, and graph traversals to reduce latency and cost for repeated queries.
Index ¶
- func GenerateEmbeddingKey(text string) string
- func GenerateGraphTraversalKey(entityID string, traversalType string, depth int) string
- func GenerateQueryKey(query string, filters map[string]interface{}) string
- type Cache
- type CacheConfig
- type CacheEntry
- type CacheManager
- type CacheStats
- type CacheWarmer
- type EmbeddingCache
- func (ec *EmbeddingCache) Clear()
- func (ec *EmbeddingCache) Delete(text string)
- func (ec *EmbeddingCache) Get(text string) ([]float32, bool)
- func (ec *EmbeddingCache) InvalidateByPrefix(prefix string)
- func (ec *EmbeddingCache) Set(text string, embedding []float32, ttl time.Duration)
- func (ec *EmbeddingCache) Stats() CacheStats
- type EvictionPolicy
- type GraphCache
- func (gc *GraphCache) Clear()
- func (gc *GraphCache) Delete(entityID string, traversalType string, depth int)
- func (gc *GraphCache) Get(entityID string, traversalType string, depth int) (*GraphTraversalResult, bool)
- func (gc *GraphCache) InvalidateByEntity(entityID string)
- func (gc *GraphCache) Set(entityID string, traversalType string, depth int, result *GraphTraversalResult, ...)
- func (gc *GraphCache) Stats() CacheStats
- type GraphTraversalResult
- type LRUCache
- type MultiLevelCache
- type QueryCache
- func (qc *QueryCache) Clear()
- func (qc *QueryCache) Delete(query string, filters map[string]interface{})
- func (qc *QueryCache) Get(query string, filters map[string]interface{}) (*QueryResult, bool)
- func (qc *QueryCache) InvalidateByPrefix(prefix string)
- func (qc *QueryCache) Set(query string, filters map[string]interface{}, result *QueryResult, ...)
- func (qc *QueryCache) Stats() CacheStats
- type QueryResult
- type WarmRequest
- type WarmStats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateEmbeddingKey ¶
GenerateEmbeddingKey generates a cache key for an embedding input.
func GenerateGraphTraversalKey ¶
GenerateGraphTraversalKey generates a cache key for a graph traversal query.
func GenerateQueryKey ¶
GenerateQueryKey generates a cache key for a query and its filters.
Types ¶
type Cache ¶
type Cache interface {
// Get retrieves a value from the cache by key.
Get(key string) (interface{}, bool)
// Set stores a value in the cache with the given key and TTL.
Set(key string, value interface{}, ttl time.Duration)
// Delete removes a value from the cache by key.
Delete(key string)
// Clear removes all values from the cache.
Clear()
// Stats returns cache statistics.
Stats() CacheStats
}
Cache defines the interface for all cache implementations.
type CacheConfig ¶
type CacheConfig struct {
MaxSize int
DefaultTTL time.Duration
EvictionPolicy EvictionPolicy
}
CacheConfig holds configuration for cache instances.
func DefaultCacheConfig ¶
func DefaultCacheConfig() CacheConfig
DefaultCacheConfig returns default configuration for caches.
type CacheEntry ¶
type CacheEntry struct {
Value interface{}
ExpiresAt time.Time
CreatedAt time.Time
LastAccessed time.Time
}
CacheEntry represents a single entry in the cache.
func (*CacheEntry) IsExpired ¶
func (e *CacheEntry) IsExpired() bool
IsExpired returns true if the cache entry has expired.
type CacheManager ¶
type CacheManager struct {
// contains filtered or unexported fields
}
CacheManager manages multiple cache instances.
func NewCacheManager ¶
func NewCacheManager(config CacheConfig) *CacheManager
NewCacheManager creates a new CacheManager with default configuration.
func (*CacheManager) DeleteCache ¶
func (m *CacheManager) DeleteCache(name string)
DeleteCache removes a cache by name.
func (*CacheManager) GetCache ¶
func (m *CacheManager) GetCache(name string) Cache
GetCache returns a cache by name, creating it if it doesn't exist.
func (*CacheManager) Stats ¶
func (m *CacheManager) Stats() map[string]CacheStats
Stats returns aggregated statistics for all caches.
type CacheStats ¶
CacheStats contains statistics about cache performance.
type CacheWarmer ¶
type CacheWarmer struct {
// contains filtered or unexported fields
}
CacheWarmer provides cache warming strategies to pre-populate the cache.
func NewCacheWarmer ¶
func NewCacheWarmer(cache Cache) *CacheWarmer
NewCacheWarmer creates a new CacheWarmer.
func (*CacheWarmer) AddRequest ¶
func (cw *CacheWarmer) AddRequest(req WarmRequest)
AddRequest adds a request to warm the cache.
func (*CacheWarmer) ClearPending ¶
func (cw *CacheWarmer) ClearPending()
ClearPending clears all pending warm requests.
func (*CacheWarmer) Stats ¶
func (cw *CacheWarmer) Stats() WarmStats
Stats returns cache warming statistics.
type EmbeddingCache ¶
type EmbeddingCache struct {
// contains filtered or unexported fields
}
EmbeddingCache provides caching for embedding vectors to avoid redundant computation.
func NewEmbeddingCache ¶
func NewEmbeddingCache(maxSize int) *EmbeddingCache
NewEmbeddingCache creates a new EmbeddingCache with default configuration.
func (*EmbeddingCache) Clear ¶
func (ec *EmbeddingCache) Clear()
Clear removes all cached embeddings.
func (*EmbeddingCache) Delete ¶
func (ec *EmbeddingCache) Delete(text string)
Delete removes a cached embedding.
func (*EmbeddingCache) Get ¶
func (ec *EmbeddingCache) Get(text string) ([]float32, bool)
Get retrieves a cached embedding vector.
func (*EmbeddingCache) InvalidateByPrefix ¶
func (ec *EmbeddingCache) InvalidateByPrefix(prefix string)
InvalidateByPrefix invalidates all cached embeddings matching a prefix.
func (*EmbeddingCache) Set ¶
func (ec *EmbeddingCache) Set(text string, embedding []float32, ttl time.Duration)
Set stores an embedding vector in the cache.
func (*EmbeddingCache) Stats ¶
func (ec *EmbeddingCache) Stats() CacheStats
Stats returns cache statistics.
type EvictionPolicy ¶
type EvictionPolicy int
EvictionPolicy defines the cache eviction strategy.
const ( // EvictionLRU uses Least Recently Used eviction. EvictionLRU EvictionPolicy = iota // EvictionLFU uses Least Frequently Used eviction. EvictionLFU // EvictionFIFO uses First In First Out eviction. EvictionFIFO )
type GraphCache ¶
type GraphCache struct {
// contains filtered or unexported fields
}
GraphCache provides caching for graph traversal results.
func NewGraphCache ¶
func NewGraphCache(maxSize int) *GraphCache
NewGraphCache creates a new GraphCache with default configuration.
func (*GraphCache) Clear ¶
func (gc *GraphCache) Clear()
Clear removes all cached graph traversal results.
func (*GraphCache) Delete ¶
func (gc *GraphCache) Delete(entityID string, traversalType string, depth int)
Delete removes a cached graph traversal result.
func (*GraphCache) Get ¶
func (gc *GraphCache) Get(entityID string, traversalType string, depth int) (*GraphTraversalResult, bool)
Get retrieves a cached graph traversal result.
func (*GraphCache) InvalidateByEntity ¶
func (gc *GraphCache) InvalidateByEntity(entityID string)
InvalidateByEntity invalidates all cached results for a specific entity.
func (*GraphCache) Set ¶
func (gc *GraphCache) Set(entityID string, traversalType string, depth int, result *GraphTraversalResult, ttl time.Duration)
Set stores a graph traversal result in the cache.
type GraphTraversalResult ¶
type GraphTraversalResult struct {
Query string
Results []*graph.Entity
Timestamp time.Time
Latency time.Duration
}
GraphTraversalResult represents a cached graph traversal result.
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache implements a Least Recently Used (LRU) cache with TTL support.
func NewLRUCache ¶
func NewLRUCache(config CacheConfig) *LRUCache
NewLRUCache creates a new LRU cache with the given configuration.
type MultiLevelCache ¶
type MultiLevelCache struct {
L1 Cache // In-memory (fast, small)
L2 Cache // Can be disk-based or larger in-memory (slower, larger)
}
MultiLevelCache provides multi-level caching with L1 (fast, small) and L2 (slower, larger) tiers.
func NewMultiLevelCache ¶
func NewMultiLevelCache(l1, l2 Cache) *MultiLevelCache
NewMultiLevelCache creates a new MultiLevelCache with L1 and L2 caches.
func (*MultiLevelCache) Clear ¶
func (mlc *MultiLevelCache) Clear()
Clear removes all values from both L1 and L2 caches.
func (*MultiLevelCache) Delete ¶
func (mlc *MultiLevelCache) Delete(key string)
Delete removes a value from both L1 and L2 caches.
func (*MultiLevelCache) Get ¶
func (mlc *MultiLevelCache) Get(key string) (interface{}, bool)
Get retrieves a value from the cache, checking L1 first, then L2.
func (*MultiLevelCache) Set ¶
func (mlc *MultiLevelCache) Set(key string, value interface{}, ttl time.Duration)
Set stores a value in both L1 and L2 caches.
func (*MultiLevelCache) Stats ¶
func (mlc *MultiLevelCache) Stats() CacheStats
Stats returns aggregated statistics from both L1 and L2 caches.
type QueryCache ¶
type QueryCache struct {
// contains filtered or unexported fields
}
QueryCache provides caching for query results.
func NewQueryCache ¶
func NewQueryCache(maxSize int) *QueryCache
NewQueryCache creates a new QueryCache with default configuration.
func (*QueryCache) Delete ¶
func (qc *QueryCache) Delete(query string, filters map[string]interface{})
Delete removes a cached query result.
func (*QueryCache) Get ¶
func (qc *QueryCache) Get(query string, filters map[string]interface{}) (*QueryResult, bool)
Get retrieves a cached query result.
func (*QueryCache) InvalidateByPrefix ¶
func (qc *QueryCache) InvalidateByPrefix(prefix string)
InvalidateByPrefix invalidates all cached results matching a query prefix.
func (*QueryCache) Set ¶
func (qc *QueryCache) Set(query string, filters map[string]interface{}, result *QueryResult, ttl time.Duration)
Set stores a query result in the cache.
type QueryResult ¶
type QueryResult struct {
Query string
Results []interface{}
Timestamp time.Time
Latency time.Duration
}
QueryResult represents a cached query result.