cache

package
v0.101.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCacheKey

func GenerateCacheKey(components ...string) string

GenerateCacheKey generates a cache key from components.

func GenerateCacheKeyWithHash

func GenerateCacheKeyWithHash(components ...string) string

GenerateCacheKeyWithHash generates a cache key with hash for uniqueness.

func GenerateQueryKey

func GenerateQueryKey(userID int, query string, limit int, strategy string) string

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.

func KeyHash

func KeyHash(key string) string

KeyHash generates a SHA256 hash of the key for obfuscation.

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 New

func New(config Config) *Cache

New creates a new memory cache with the given configuration.

func NewDefault

func NewDefault() *Cache

NewDefault creates a new memory cache with default configuration.

func (*Cache) Clear

func (c *Cache) Clear(_ context.Context)

Clear removes all values from the cache.

func (*Cache) Close

func (c *Cache) Close() error

Close stops the cache cleanup goroutine.

func (*Cache) Delete

func (c *Cache) Delete(_ context.Context, key string)

Delete removes a value from the cache.

func (*Cache) Get

func (c *Cache) Get(_ context.Context, key string) (any, bool)

Get retrieves a value from the cache.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, value any)

Set adds a value to the cache with the default TTL.

func (*Cache) SetWithTTL

func (c *Cache) SetWithTTL(_ context.Context, key string, value any, ttl time.Duration)

SetWithTTL adds a value to the cache with a custom TTL.

func (*Cache) Size

func (c *Cache) Size() int64

Size returns the number of items in the cache.

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 L3Fetcher

type L3Fetcher func(ctx context.Context, key string) (any, error)

L3Fetcher is the function to fetch data from the database (L3).

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) Delete

func (n *NilRedisCache) Delete(ctx context.Context, key string)

func (*NilRedisCache) Get

func (n *NilRedisCache) Get(ctx context.Context, key string) (any, bool)

func (*NilRedisCache) Set

func (n *NilRedisCache) Set(ctx context.Context, key string, value any)

func (*NilRedisCache) SetWithTTL

func (n *NilRedisCache) SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration)

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:

  1. Add redis dependency: go get github.com/redis/go-redis/v9
  2. Uncomment the implementation below
  3. 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.

func (*SemanticCache) Get

func (s *SemanticCache) Get(ctx context.Context, query string) (any, bool)

Get retrieves a cached result, either by exact key or semantic similarity.

func (*SemanticCache) Set

func (s *SemanticCache) Set(ctx context.Context, query string, results any) error

Set stores a query result in the semantic 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) Close

func (t *TieredCache) Close() error

Close closes all cache connections.

func (*TieredCache) Delete

func (t *TieredCache) Delete(ctx context.Context, key string)

Delete removes a value from both L1 and L2.

func (*TieredCache) Get

func (t *TieredCache) Get(ctx context.Context, key string, fetcher L3Fetcher) (any, bool)

Get retrieves a value from the cache, checking L1, then L2, then L3.

func (*TieredCache) Invalidate

func (t *TieredCache) Invalidate(ctx context.Context, key string, fetcher L3Fetcher) error

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

func (t *TieredCache) SetWithTTL(ctx context.Context, key string, value any, ttl time.Duration)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL