cache

package
v1.64.4 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package cache provides in-memory caches for parsed ASTs and LLM responses.

Both caches use LRU eviction and are safe for concurrent access.

Index

Constants

View Source
const (
	DefaultParseCacheSize = 5000
	DefaultLLMCacheSize   = 500
	DefaultLLMTTL         = time.Hour
)

Default cache sizes.

Variables

This section is empty.

Functions

func Key

func Key(parts ...string) string

Key generates a deterministic cache key from parts using FNV-128a.

func PromptHash

func PromptHash(systemPrompt, userPrompt string) uint64

PromptHash computes the FNV-1a hash of system + user prompt pair.

Types

type LLMCache

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

LLMCache caches LLM completion responses keyed by FNV-1a hash of prompts.

func NewLLMCache

func NewLLMCache(maxSize int, ttl time.Duration) *LLMCache

NewLLMCache creates an LLM response cache with the given size and TTL.

func (*LLMCache) Get

func (c *LLMCache) Get(key uint64) (string, bool)

Get returns a cached LLM response if present and not expired.

func (*LLMCache) Put

func (c *LLMCache) Put(key uint64, response string)

Put stores an LLM response. Evicts the least-frequently-used entry if at capacity.

func (*LLMCache) Stats

func (c *LLMCache) Stats() Stats

Stats returns current cache statistics.

type LRU

type LRU[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRU is a generic O(1) LRU cache backed by a doubly-linked list. It is NOT safe for concurrent use; callers must hold their own mutex. Zero value is invalid; use NewLRU.

func NewLRU

func NewLRU[K comparable, V any](maxSize int) *LRU[K, V]

NewLRU creates an LRU cache with the given maximum capacity. maxSize must be > 0.

func (*LRU[K, V]) Delete

func (c *LRU[K, V]) Delete(key K)

Delete removes key from the cache. No-op if absent.

func (*LRU[K, V]) Get

func (c *LRU[K, V]) Get(key K) (V, bool)

Get returns the value for key and true, or the zero value and false if absent. Accessing a key moves it to the most-recently-used position.

func (*LRU[K, V]) Len

func (c *LRU[K, V]) Len() int

Len returns the number of entries currently in the cache.

func (*LRU[K, V]) Set

func (c *LRU[K, V]) Set(key K, value V)

Set inserts or updates key with value, evicting the LRU entry if at capacity.

type ParseCache

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

ParseCache caches tree-sitter parse results and call sites keyed by absolute file path and includeBody mode: a body-mode mismatch is always a miss, so both parse modes coexist independently in the same cache.

func NewParseCache

func NewParseCache(maxSize int) *ParseCache

NewParseCache creates a parse cache with the given maximum entry count.

func (*ParseCache) Get

func (c *ParseCache) Get(path string, modTime, size int64, includeBody, includeTypeRels bool) (*parser.ParseResult, []parser.CallSite)

Get returns a cached parse result and its call sites for path, scoped to the given includeBody and includeTypeRels modes. Returns (nil, nil) if not cached, stale (modTime/size mismatch), or cached under a different mode.

func (*ParseCache) Put

func (c *ParseCache) Put(path string, modTime, size int64, includeBody, includeTypeRels bool, result *parser.ParseResult, calls []parser.CallSite)

Put stores a parse result and its call sites for path, scoped to the given includeBody and includeTypeRels modes. Evicts the least-recently-used entry if at capacity.

func (*ParseCache) Stats

func (c *ParseCache) Stats() Stats

Stats returns current cache statistics.

type Stats

type Stats struct {
	Hits    int64 `json:"hits"`
	Misses  int64 `json:"misses"`
	Entries int   `json:"entries"`
}

Stats holds cache hit/miss counters.

type TTLLRU

type TTLLRU[K comparable, V any] struct {
	// contains filtered or unexported fields
}

TTLLRU is a generic, concurrency-safe LRU cache with a per-entry TTL: Set stamps time.Now() at insertion under the cache's default TTL, SetWithTTL overrides that TTL for one entry, and a read past its TTL is lazily evicted rather than returned.

Extracted from the TTL+LRU skeleton that had accreted independently across internal/callgraph/repo_cache.go, internal/compare/churn_cache.go, internal/compare/coupling_cache.go, and internal/federate/touches_cache.go (mu.Lock → lru.Get → lazy-expiry-delete → return / mu.Lock → lru.Set with a time.Now() stamp, repeated four times) — new TTL+LRU call sites should use this instead of hand-rolling a fifth copy. The four pre-existing sites are left as-is here (separate migration, tracked as debt). (A fifth call site, goanalysis.CachedLoadPackages, was removed in issue #747 — it pinned the go/types arena for the cache TTL and OOM-killed the indexer; the load is now passed through the request seam instead of cached globally.)

Unlike the plain LRU (which pushes its mutex to the caller), TTLLRU holds its own — every method here is safe for concurrent use.

func NewTTLLRU

func NewTTLLRU[K comparable, V any](maxSize int, ttl time.Duration) *TTLLRU[K, V]

NewTTLLRU creates a TTL+LRU cache with the given max capacity and default per-entry TTL. The default applies to every Set call; SetWithTTL overrides it for a single entry (e.g. a shorter TTL for a cached failure than for a cached success).

func (*TTLLRU[K, V]) Delete

func (c *TTLLRU[K, V]) Delete(key K)

Delete removes key from the cache. No-op if absent.

func (*TTLLRU[K, V]) Get

func (c *TTLLRU[K, V]) Get(key K) (V, bool)

Get returns the value for key and true, or the zero value and false if absent or past its TTL — an expired entry is lazily deleted on read.

func (*TTLLRU[K, V]) Len

func (c *TTLLRU[K, V]) Len() int

Len returns the number of entries currently in the cache, including any not-yet-lazily-evicted expired entries.

func (*TTLLRU[K, V]) Set

func (c *TTLLRU[K, V]) Set(key K, value V)

Set inserts or updates key with value under the cache's default TTL.

func (*TTLLRU[K, V]) SetWithTTL

func (c *TTLLRU[K, V]) SetWithTTL(key K, value V, ttl time.Duration)

SetWithTTL inserts or updates key with value under an entry-specific TTL, overriding the cache's default for this call only.

A ttl <= 0 is a deliberate no-op: the entry is never stored, so every subsequent Get misses and every caller re-attempts from scratch. This is how a caller-budget-specific outcome (e.g. one caller's context deadline expiring) can be excluded from caching entirely, rather than cached under a token TTL that would still let it poison a different caller with a longer remaining budget.

Jump to

Keyboard shortcuts

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