Documentation
¶
Index ¶
- func GenerateCacheKey(components ...string) string
- func GenerateCacheKeyWithHash(components ...string) string
- func GenerateQueryKey(userID int, query string, limit int, strategy string) string
- func IsRedisEnabled() bool
- func KeyHash(key string) string
- type Cache
- func (c *Cache) Clear(_ context.Context)
- func (c *Cache) Close() error
- func (c *Cache) Delete(_ context.Context, key string)
- func (c *Cache) Get(_ context.Context, key string) (any, bool)
- func (c *Cache) Set(ctx context.Context, key string, value any)
- func (c *Cache) SetWithTTL(_ context.Context, key string, value any, ttl time.Duration)
- func (c *Cache) Size() int64
- type CacheStats
- type Config
- type Interface
- type L3Fetcher
- type NilRedisCache
- func (n *NilRedisCache) Clear(ctx context.Context)
- func (n *NilRedisCache) Close() error
- func (n *NilRedisCache) Delete(ctx context.Context, key string)
- func (n *NilRedisCache) Get(ctx context.Context, key string) (any, bool)
- func (n *NilRedisCache) Set(ctx context.Context, key string, value any)
- func (n *NilRedisCache) SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration)
- type RedisCacheConfig
- type RedisCacheInterface
- type RedisCacheStats
- type SemanticCache
- type SemanticCacheEntry
- type TieredCache
- func (t *TieredCache) Clear(ctx context.Context)
- func (t *TieredCache) Close() error
- func (t *TieredCache) Delete(ctx context.Context, key string)
- func (t *TieredCache) Get(ctx context.Context, key string, fetcher L3Fetcher) (any, bool)
- func (t *TieredCache) Invalidate(ctx context.Context, key string, fetcher L3Fetcher) error
- func (t *TieredCache) Set(ctx context.Context, key string, value any)
- func (t *TieredCache) SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration)
- func (t *TieredCache) Stats() map[string]interface{}
- type TieredCacheConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateCacheKey ¶
GenerateCacheKey generates a cache key from components.
func GenerateCacheKeyWithHash ¶
GenerateCacheKeyWithHash generates a cache key with hash for uniqueness.
func GenerateQueryKey ¶
GenerateQueryKey generates a consistent cache key from query components.
func IsRedisEnabled ¶
func IsRedisEnabled() bool
IsRedisEnabled checks if Redis caching should be enabled based on environment. Returns true if DIVINESENSE_CACHE_REDIS_ADDR is set.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a thread-safe in-memory cache with TTL and memory management.
func NewDefault ¶
func NewDefault() *Cache
NewDefault creates a new memory cache with default configuration.
func (*Cache) SetWithTTL ¶
SetWithTTL adds a value to the cache with a custom TTL.
type CacheStats ¶
type CacheStats struct {
Metadata map[string]interface{} `json:"metadata"`
L1Size int64 `json:"l1_size"`
L2Size int64 `json:"l2_size"`
L1HitRate float64 `json:"l1_hit_rate"`
L2HitRate float64 `json:"l2_hit_rate"`
SemanticHit int64 `json:"semantic_hits"`
TotalHits int64 `json:"total_hits"`
TotalMisses int64 `json:"total_misses"`
}
CacheStats represents combined cache statistics.
func GetCacheStats ¶
func GetCacheStats(tiered *TieredCache, semantic *SemanticCache) *CacheStats
GetCacheStats returns statistics from all cache layers. Note: Hit/miss tracking is not currently implemented; returns 0 for those fields. To enable hit/miss tracking, add atomic counters to Cache struct.
type Config ¶
type Config struct {
OnEviction func(key string, value any)
DefaultTTL time.Duration
CleanupInterval time.Duration
MaxItems int
}
Config contains options for configuring a cache.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a default configuration for the cache.
type Interface ¶
type Interface interface {
// Set adds a value to the cache with the default TTL.
Set(ctx context.Context, key string, value any)
// SetWithTTL adds a value to the cache with a custom TTL.
SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration)
// Get retrieves a value from the cache.
Get(ctx context.Context, key string) (any, bool)
// Delete removes a value from the cache.
Delete(ctx context.Context, key string)
// Clear removes all values from the cache.
Clear(ctx context.Context)
// Size returns the number of items in the cache.
Size() int64
// Close stops all background tasks and releases resources.
Close() error
}
Interface defines the operations a cache must support.
type NilRedisCache ¶
type NilRedisCache struct{}
NilRedisCache is a no-op implementation of RedisCacheInterface. This allows the tiered cache to work without Redis.
func NewNilRedisCache ¶
func NewNilRedisCache() *NilRedisCache
NewNilRedisCache creates a no-op Redis cache.
func (*NilRedisCache) Clear ¶
func (n *NilRedisCache) Clear(ctx context.Context)
func (*NilRedisCache) Close ¶
func (n *NilRedisCache) Close() error
func (*NilRedisCache) SetWithTTL ¶
type RedisCacheConfig ¶
type RedisCacheConfig struct {
Addr string
Password string
KeyPrefix string
DB int
DefaultTTL time.Duration
PoolSize int
MinIdleConns int
}
RedisCacheConfig holds the Redis connection configuration.
func DefaultRedisConfig ¶
func DefaultRedisConfig() *RedisCacheConfig
DefaultRedisConfig returns the default Redis configuration.
func RedisConfigFromEnv ¶
func RedisConfigFromEnv() *RedisCacheConfig
RedisConfigFromEnv creates Redis config from environment variables. Environment variables:
- DIVINESENSE_CACHE_REDIS_ADDR: Redis address (default: localhost:6379)
- DIVINESENSE_CACHE_REDIS_PASSWORD: Redis password (default: "")
- DIVINESENSE_CACHE_REDIS_DB: Redis DB number (default: 0)
- DIVINESENSE_CACHE_REDIS_PREFIX: Key prefix (default: "divinesense:")
type RedisCacheInterface ¶
type RedisCacheInterface interface {
Set(ctx context.Context, key string, value any)
SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration)
Get(ctx context.Context, key string) (any, bool)
Delete(ctx context.Context, key string)
Clear(ctx context.Context)
Close() error
}
RedisCacheInterface defines the interface for Redis L2 cache. This is an optional interface - the system works fine with just memory cache. Redis is OPTIONAL and only needed for:
- Multi-instance deployments
- Cross-process cache sharing
- Persistent cache across restarts
To enable Redis support:
- Add redis dependency: go get github.com/redis/go-redis/v9
- Uncomment the implementation below
- Build with redis tag: go build -tags redis
type RedisCacheStats ¶
type RedisCacheStats struct {
Keyspace string `json:"keyspace"`
KeyCount int64 `json:"key_count"`
Memory int64 `json:"memory_bytes"`
HitRate float64 `json:"hit_rate"`
}
RedisCacheStats represents Redis cache statistics.
type SemanticCache ¶
type SemanticCache struct {
// contains filtered or unexported fields
}
SemanticCache provides semantic caching based on vector similarity. It caches query results and can return semantically similar cached results.
func NewSemanticCache ¶
func NewSemanticCache(embedding ai.EmbeddingService, threshold float32, maxItems int) *SemanticCache
NewSemanticCache creates a new semantic cache.
func (*SemanticCache) Delete ¶
func (s *SemanticCache) Delete(ctx context.Context, query string)
Delete removes a query from the cache.
type SemanticCacheEntry ¶
type SemanticCacheEntry struct {
Timestamp time.Time `json:"timestamp"`
Results any `json:"results"`
Query string `json:"query"`
Embedding []float32 `json:"embedding"`
}
SemanticCacheEntry represents a cached semantic search result.
type TieredCache ¶
type TieredCache struct {
// contains filtered or unexported fields
}
TieredCache implements a three-tier caching strategy: - L1: In-memory cache (fast, small, DEFAULT) - L2: Redis cache (moderate, shared, OPTIONAL) - L3: Database callback (slow, persistent)
DEFAULT BEHAVIOR (personal system):
- L1 memory cache enabled (1000 items, 5min TTL)
- L2 Redis disabled
TO ENABLE REDIS (multi-instance):
- Set DIVINESENSE_CACHE_REDIS_ADDR environment variable
func NewTieredCache ¶
func NewTieredCache(config *TieredCacheConfig) (*TieredCache, error)
NewTieredCache creates a new three-tier cache.
func (*TieredCache) Clear ¶
func (t *TieredCache) Clear(ctx context.Context)
Clear clears all caches.
func (*TieredCache) Delete ¶
func (t *TieredCache) Delete(ctx context.Context, key string)
Delete removes a value from both L1 and L2.
func (*TieredCache) Invalidate ¶
Invalidate removes a value and optionally refreshes it.
func (*TieredCache) Set ¶
func (t *TieredCache) Set(ctx context.Context, key string, value any)
Set stores a value in both L1 and L2.
func (*TieredCache) SetWithTTL ¶
SetWithTTL stores a value with custom TTL.
func (*TieredCache) Stats ¶
func (t *TieredCache) Stats() map[string]interface{}
Stats returns cache statistics.
type TieredCacheConfig ¶
type TieredCacheConfig struct {
L1MaxItems int // Max items in L1 memory cache
L1TTL time.Duration // TTL for L1 cache entries
L2TTL time.Duration // TTL for L2 Redis cache entries
EnableL1 bool // Enable L1 memory cache (default: true)
EnableL2 bool // Enable L2 Redis cache (default: false, auto-enabled if DIVINESENSE_CACHE_REDIS_ADDR set)
}
TieredCacheConfig holds the configuration for the tiered cache.
func DefaultTieredConfig ¶
func DefaultTieredConfig() *TieredCacheConfig
DefaultTieredConfig returns the default tiered cache configuration. For personal systems: L1 enabled, L2 disabled.