cache

package
v1.6.0-beta.4 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeKey

func ComputeKey(apiKeyID string, configVersion int64, req *pb.GenerateTextRequest) string

ComputeKey returns a deterministic hash key for an exact-match request. The key includes the calling client's APIKeyID (tenant isolation), the configuration snapshot version (invalidation on rule changes), and canonical request parameters.

func SemanticText

func SemanticText(req *pb.GenerateTextRequest) (string, bool)

SemanticText returns the utterance compared in latent space: the final user message, or the request instruction when there are no messages. Requests ending in tool traffic or non-text parts are not semantically cacheable.

Types

type Cache

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

Cache provides encrypted exact-match caching for LLM completions in Redis.

func New

func New(client *redis.Client, keyring *crypto.Keyring, ttl time.Duration, maxBytes int, prefix string) *Cache

New creates a new Cache instance.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, apiKeyID string, configVersion int64, req *pb.GenerateTextRequest) (*pb.GenerateTextResponse, bool, error)

Get retrieves and decrypts a cached response for the given request.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, apiKeyID string, configVersion int64, req *pb.GenerateTextRequest, resp *pb.GenerateTextResponse) error

Set encrypts and stores a response in Redis under the deterministic key.

type SemanticCache

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

SemanticCache serves responses for prompts that mean the same thing as an earlier one. Everything except the final user utterance must match exactly (the bucket); only that utterance is compared in latent space.

func NewSemantic

func NewSemantic(client *redis.Client, keyring *crypto.Keyring, enc latent.Encoder, opts SemanticOptions) *SemanticCache

NewSemantic builds a semantic cache over Redis; vectors and responses are encrypted at rest like the exact-match cache, since embeddings can be inverted.

func (*SemanticCache) Get

func (c *SemanticCache) Get(ctx context.Context, apiKeyID string, configVersion int64, req *pb.GenerateTextRequest) (*pb.GenerateTextResponse, float32, bool, error)

Get returns the cached response whose prompt is most similar to req, with its similarity, when that similarity reaches the threshold.

func (*SemanticCache) Set

func (c *SemanticCache) Set(ctx context.Context, apiKeyID string, configVersion int64, req *pb.GenerateTextRequest, resp *pb.GenerateTextResponse) error

Set indexes resp under the latent vector of req's utterance. Responses with tool calls are skipped: their arguments echo specifics of the exact prompt.

type SemanticOptions

type SemanticOptions struct {
	// Threshold is the minimum cosine similarity that counts as a hit.
	Threshold float64
	// MaxEntries bounds how many vectors one bucket keeps and a lookup scans.
	MaxEntries int
	TTL        time.Duration
	MaxBytes   int
	Prefix     string
}

SemanticOptions tunes the latent-space cache.

Jump to

Keyboard shortcuts

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