cache

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: Apache-2.0 Imports: 8 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

This section is empty.

Types

type Entry

type Entry struct {
	Body      []byte
	Header    map[string][]string
	ExpiresAt time.Time
	// RequestHash stores the SHA256 of the request body so semantic hits can
	// compare the request, not just the response (Bifrost-style). Empty = exact key.
	RequestHash string
	// RequestWords are the significant words of the last user message, used by
	// the cheap semantic FindSimilar (Jaccard overlap) without embeddings.
	RequestWords []string
	// PromptCacheHitTokens approximates deepseek/gemini prompt_cache_hit_tokens.
	PromptCacheHitTokens  int
	PromptCacheMissTokens int
}

type Stats

type Stats struct {
	Size        int     `json:"size"`
	MaxSize     int     `json:"max_size"`
	HitRate     float64 `json:"hit_rate"`
	ExactHits   uint64  `json:"exact_hits"`
	SimilarHits uint64  `json:"similar_hits"`
	Misses      uint64  `json:"misses"`
}

Stats captures cache hit/miss counters for the Token Lens widget.

type TTLResponseCache

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

func NewTTLResponseCache

func NewTTLResponseCache(maxSize int) *TTLResponseCache

func (*TTLResponseCache) Clear

func (c *TTLResponseCache) Clear()

func (*TTLResponseCache) FindSimilar

func (c *TTLResponseCache) FindSimilar(model string, body []byte, threshold float64) (Entry, string, bool)

FindSimilar returns the most similar cached entry by Jaccard on the request body. threshold 0.8 means 80% word overlap on last user message required. This is a cheap semantic-like cache (Bifrost-style) — no embeddings needed.

func (*TTLResponseCache) Get

func (c *TTLResponseCache) Get(key string) (Entry, bool)

func (*TTLResponseCache) KeyFor

func (c *TTLResponseCache) KeyFor(model string, body []byte) string

func (*TTLResponseCache) LoadFromFile

func (c *TTLResponseCache) LoadFromFile(path string) error

LoadFromFile restores cache from disk.

func (*TTLResponseCache) SaveToFile

func (c *TTLResponseCache) SaveToFile(path string) error

SaveToFile persists cache to disk (best-effort, for restart).

func (*TTLResponseCache) Set

func (c *TTLResponseCache) Set(key string, body []byte, header map[string][]string, ttl time.Duration)

Set stores an entry. requestBody is the incoming chat payload (used for semantic RequestHash); pass nil for exact-key-only caches.

func (*TTLResponseCache) SetWithRequest

func (c *TTLResponseCache) SetWithRequest(key string, body []byte, header map[string][]string, ttl time.Duration, requestBody []byte, cacheHit, cacheMiss int)

SetWithRequest stores an entry and keeps the request hash + prompt cache accounting for semantic lookups and DeepSeek-style prompt_cache stats.

func (*TTLResponseCache) Size

func (c *TTLResponseCache) Size() int

func (*TTLResponseCache) Stats

func (c *TTLResponseCache) Stats() Stats

Jump to

Keyboard shortcuts

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