cache

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 9 Imported by: 0

README

embedding/cache

embedding/cache adds caller-owned, provider-neutral caching to an embedding.Embedder. Give it a cache identity that changes whenever embedding output compatibility changes, and provide a Store implementation suitable for your process and concurrency needs.

store := inmem.New()
cached, err := embeddingcache.New(base, store, embeddingcache.Config{
	Identity: "openai:text-embedding-3-small:1536:input-v1",
})

Entries are keyed by the cache identity, requested model, and exact input bytes. The decorator preserves request order, groups duplicate inputs, validates cached and generated vectors, and clones vectors at ownership boundaries. It does not provide persistence, eviction, invalidation, or singleflight.

embedding/cache/inmem supplies an optional concurrency-safe Store for process-local caches. Its zero value works, and inmem.New() constructs an initialized Store. Use a different Store when entries must survive the process or need capacity, TTL, or eviction policy.

Documentation

Overview

Package cache decorates an embedding.Embedder with an explicit, caller-owned Store. It caches individual input vectors while preserving embedding batch order and resolved model identity.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilEmbedder indicates that New received a nil Embedder.
	ErrNilEmbedder = errors.New("nil embedder")
	// ErrNilStore indicates that New received a nil Store.
	ErrNilStore = errors.New("nil store")
	// ErrInvalidIdentity indicates that New received a blank cache identity.
	ErrInvalidIdentity = errors.New("invalid cache identity")
	// ErrModelMismatch indicates that entries in one batch resolve to different models.
	ErrModelMismatch = errors.New("embedding model mismatch")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Identity namespaces entries by the caller-owned embedding-output contract.
	Identity string
}

Config configures an Embedder.

type Embedder

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

Embedder decorates a base embedding.Embedder with a caller-provided Store. Its state is immutable after construction.

func New

func New(base embedding.Embedder, store Store, cfg Config) (*Embedder, error)

New constructs a Store-backed decorator without performing dependency I/O.

Example
package main

import (
	"context"
	"fmt"

	"github.com/dotcommander/reliquary/embedding"
	"github.com/dotcommander/reliquary/embedding/cache"
)

func main() {
	base := exampleEmbedder{}
	store := &exampleStore{entries: make(map[string]cache.Entry)}
	cached, err := cache.New(base, store, cache.Config{Identity: "example:v1"})
	if err != nil {
		panic(err)
	}

	result, err := cached.Embed(context.Background(), embedding.Request{Inputs: []string{"hello"}})
	fmt.Println(err == nil, result.Model.Name, len(result.Vectors))
}

type exampleEmbedder struct{}

func (exampleEmbedder) Embed(_ context.Context, request embedding.Request) (embedding.Result, error) {
	vectors := make([]embedding.Vector, len(request.Inputs))
	for index, input := range request.Inputs {
		vectors[index] = embedding.Vector{float32(len(input))}
	}
	return embedding.Result{Model: embedding.ModelRef{Name: "example", Dim: 1}, Vectors: vectors}, nil
}

type exampleStore struct {
	entries map[string]cache.Entry
}

func (s *exampleStore) Get(_ context.Context, key string) (cache.Entry, bool, error) {
	entry, ok := s.entries[key]
	return entry, ok, nil
}

func (s *exampleStore) Set(_ context.Context, key string, entry cache.Entry) error {
	s.entries[key] = entry
	return nil
}
Output:
true example 1

func (*Embedder) Embed

func (e *Embedder) Embed(ctx context.Context, request embedding.Request) (embedding.Result, error)

Embed embeds request inputs, using cached vectors where available.

type Entry

type Entry struct {
	Model  embedding.ModelRef
	Vector embedding.Vector
}

Entry is one resolved embedding cached by a Store.

type Store

type Store interface {
	Get(context.Context, string) (Entry, bool, error)
	Set(context.Context, string, Entry) error
}

Store persists individual embedding entries. Implementations must be safe for the concurrency with which callers invoke Embedder.

Directories

Path Synopsis
Package inmem provides a concurrency-safe, process-local implementation of cache.Store.
Package inmem provides a concurrency-safe, process-local implementation of cache.Store.

Jump to

Keyboard shortcuts

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