cache

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache provides semantic caching functionality.

func New

func New(cache SemanticCache, opts ...Option) *Cache

New creates a new cache service.

Required: cache. Optional (via options): WithCacheThreshold (default 0.98), WithLogger, WithCollector.

func (*Cache) Check

func (s *Cache) Check(ctx context.Context, query *core.Query) (*CheckResult, error)

Check checks if the query has a cached response.

func (*Cache) Set

func (s *Cache) Set(ctx context.Context, query *core.Query, answer string) error

Set caches a query-answer pair.

type CheckResult

type CheckResult struct {
	Hit    bool
	Answer string
}

CheckResult holds the result of a cache check.

type InMemorySemanticCache

type InMemorySemanticCache struct {
	// contains filtered or unexported fields
}

InMemorySemanticCache is a lightweight, concurrent-safe semantic cache. For production, this could be backed by Redis + Vector Search (like RedisVL).

func NewInMemorySemanticCache

func NewInMemorySemanticCache() *InMemorySemanticCache

func (*InMemorySemanticCache) Get

func (c *InMemorySemanticCache) Get(ctx context.Context, queryEmbedding []float32, threshold float32) (string, bool, error)

func (*InMemorySemanticCache) Set

func (c *InMemorySemanticCache) Set(ctx context.Context, queryEmbedding []float32, response string) error

type Option

type Option func(*Cache)

Option configures a Cache instance.

func WithCacheThreshold

func WithCacheThreshold(threshold float32) Option

WithCacheThreshold sets the similarity threshold for cache hits.

func WithCollector

func WithCollector(collector observability.Collector) Option

WithSemanticCacheCollector sets an observability collector.

func WithLogger

func WithLogger(logger logging.Logger) Option

WithSemanticCacheLogger sets a structured logger.

type SemanticCache

type SemanticCache interface {
	// Get retrieves a cached response if the semantic distance of the query vector
	// is within the given threshold (e.g., Cosine Similarity > 0.98).
	Get(ctx context.Context, queryEmbedding []float32, threshold float32) (string, bool, error)

	// Set stores the query vector and its corresponding response in the cache.
	Set(ctx context.Context, queryEmbedding []float32, response string) error
}

SemanticCache provides caching for queries based on their vector semantic similarity.

Jump to

Keyboard shortcuts

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