Skip to content
Docs

Cache Providers

Beluga AI provides a unified cache.Cache interface for key-value storage with TTL support. The cache system supports both exact-match and semantic (embedding-based) lookups, making it suitable for LLM response caching, prompt caching, and general application caching.

All cache providers implement the same interface:

type Cache interface {
Get(ctx context.Context, key string) (any, bool, error)
Set(ctx context.Context, key string, value any, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Clear(ctx context.Context) error
}

You can instantiate any provider two ways:

Via the registry (recommended for dynamic configuration):

import (
"time"
"github.com/lookatitude/beluga-ai/cache"
_ "github.com/lookatitude/beluga-ai/cache/providers/inmemory"
)
c, err := cache.New("inmemory", cache.Config{
TTL: 5 * time.Minute,
MaxSize: 1000,
})

Via direct construction (for compile-time type safety):

import "github.com/lookatitude/beluga-ai/cache/providers/inmemory"
c := inmemory.New(cache.Config{
TTL: 5 * time.Minute,
MaxSize: 1000,
})

All providers accept cache.Config:

FieldTypeDescription
TTLtime.DurationDefault time-to-live for entries
MaxSizeintMaximum number of entries (0 = unlimited)
Optionsmap[string]anyProvider-specific configuration
TTL ValueBehavior
> 0Entry expires after the given duration
0Uses the cache’s default TTL
< 0Entry never expires

The cache.SemanticCache wraps any Cache to provide similarity-based lookups using embedding vectors:

import "github.com/lookatitude/beluga-ai/cache"
c, err := cache.New("inmemory", cache.Config{TTL: 10 * time.Minute})
if err != nil {
log.Fatal(err)
}
sc := cache.NewSemanticCache(c, 0.95) // 0.95 cosine similarity threshold
// Store with embedding vector
err = sc.SetSemantic(ctx, embedding, cachedResponse)
// Retrieve by similarity
val, found, err := sc.GetSemantic(ctx, queryEmbedding, 0)

The threshold parameter controls minimum cosine similarity required for a match:

  • 0.95 — Requires very high similarity (near-exact match)
  • 0.80 — More permissive, allows paraphrased queries to hit cache
ProviderRegistry NameDescription
In-MemoryinmemoryLRU cache with TTL-based expiration

List all registered cache providers at runtime:

for _, name := range cache.List() {
fmt.Println(name)
}
Use CaseRecommended ProviderReason
Development and testinginmemoryZero dependencies, fast setup
Single-process productioninmemoryLow-latency, no network overhead
Multi-process / distributedCustom providerImplement cache.Cache with Redis, Memcached, etc.