cache

package
v0.44.0 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("cache: not found")

ErrNotFound is returned when a key is not in the cache (or expired).

View Source
var ErrRepoMapCacheMiss = errors.New("repomap cache: miss")

Functions

func ComputeProjectFingerprint

func ComputeProjectFingerprint(projectPath string) (int64, error)

ComputeProjectFingerprint computes a best-effort fingerprint based on max modTime.

It walks the project directory and finds the latest modification time across files. Certain directories are excluded to keep it fast and stable.

func LoadRepoMapFromDisk

func LoadRepoMapFromDisk(projectPath string, fingerprint int64) (string, error)

LoadRepoMapFromDisk loads a cached repo map if fingerprint matches.

func SaveRepoMapToDisk

func SaveRepoMapToDisk(projectPath string, fingerprint int64, repoMap string) error

SaveRepoMapToDisk persists repo map cache.

Types

type Cache

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

Cache is a thread-safe TTL + LRU cache.

- If disabled (Config.Enabled=false or Capacity<=0), all operations are no-op and Get returns ErrNotFound. - TTL is optional per entry; 0 means "no expiration" for that entry. - DefaultTTL is applied when Set is called with ttl=0.

This cache is in-memory only (no persistence).

func New

func New(cfg Config, clock Clock) *Cache

New creates a new cache.

If cfg.Capacity is 0 or negative, the cache behaves as disabled.

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes key from the cache.

func (*Cache) Enabled

func (c *Cache) Enabled() bool

Enabled reports whether the cache is effectively enabled.

func (*Cache) Get

func (c *Cache) Get(key string) ([]byte, error)

Get returns the cached value for key. It returns ErrNotFound if missing or expired.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of non-expired entries. It may lazily remove expired entries.

func (*Cache) Metrics

func (c *Cache) Metrics() Metrics

Metrics returns a snapshot of cache hit/miss counters.

func (*Cache) Purge

func (c *Cache) Purge()

Purge clears all entries.

func (*Cache) Set

func (c *Cache) Set(key string, value []byte, ttl time.Duration)

Set stores value for key.

ttl: - ttl > 0: expires after ttl - ttl == 0: uses Config.DefaultTTL; if that is also 0, no expiration - ttl < 0: treated as immediate expiration (effectively delete)

type Clock

type Clock interface {
	Now() time.Time
}

Clock abstracts time for testability.

NOTE: Use RealClock in production. Tests can provide a fake clock.

type Config

type Config struct {
	Enabled    bool
	Capacity   int           // max number of entries; <=0 means 0 (disabled behavior)
	DefaultTTL time.Duration // 0 means no expiration by default
}

Config defines cache behavior.

type KeyBuilder

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

KeyBuilder helps create stable cache keys.

This is intentionally minimal and does not assume any specific caching target. Callers can include provider/model/projectPath/etc. and a content hash.

Example:

kb := cache.NewKeyBuilder("prompt")
key := kb.Add("provider", "openai").Add("model", "gpt-4o").AddHash("system_prompt", prompt).String()

Output format is deterministic.

func NewKeyBuilder

func NewKeyBuilder(prefix string) *KeyBuilder

func (*KeyBuilder) Add

func (k *KeyBuilder) Add(name, value string) *KeyBuilder

func (*KeyBuilder) AddHash

func (k *KeyBuilder) AddHash(name string, content string) *KeyBuilder

func (*KeyBuilder) String

func (k *KeyBuilder) String() string

type Metrics

type Metrics struct {
	Hits   uint64
	Misses uint64
}

Metrics are best-effort counters. All fields are updated under lock.

type RealClock

type RealClock struct{}

RealClock is the production clock.

func (RealClock) Now

func (RealClock) Now() time.Time

type RepoMapCacheEntry

type RepoMapCacheEntry struct {
	ProjectPath string `json:"project_path"`
	Fingerprint int64  `json:"fingerprint"` // UnixNano
	RepoMap     string `json:"repo_map"`
}

RepoMapCacheEntry is a persisted repo map cache.

Key is derived from project path (hashed for filename safety). Fingerprint is a best-effort project change detector (max modTime across files).

NOTE: This is intentionally conservative: if fingerprint mismatches, cache is ignored. It is not cryptographically strong; it is intended for speed, not security.

Jump to

Keyboard shortcuts

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