cache

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cache provides a disk-based LRU response cache for AI provider calls. Cache key = SHA256(provider+model+prompt). Entries stored as gzip-compressed JSON files under .cloop/cache/. Supports TTL expiry and LRU eviction.

Index

Constants

View Source
const (
	DefaultTTL     = 24 * time.Hour
	DefaultMaxSize = 100
)

Variables

This section is empty.

Functions

func Key

func Key(providerName, model, prompt string) string

Key computes a deterministic cache key for a given provider, model, and prompt.

Types

type Cache

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

Cache is a disk-based LRU response cache.

func New

func New(workDir string, ttl time.Duration, maxSize int) (*Cache, error)

New creates a new Cache rooted at workDir with the given TTL and max entry count.

func (*Cache) Clear

func (c *Cache) Clear() error

Clear removes all cached entries and resets statistics.

func (*Cache) Get

func (c *Cache) Get(key string) (string, bool)

Get returns the cached response for key, or ("", false) on miss/expiry.

func (*Cache) Put

func (c *Cache) Put(key, response, providerName, model string) error

Put stores a response in the cache, evicting LRU entries if needed.

func (*Cache) Stats

func (c *Cache) Stats() Stats

Stats returns current cache statistics.

type Entry

type Entry struct {
	Key       string    `json:"key"`
	Response  string    `json:"response"`
	Provider  string    `json:"provider"`
	Model     string    `json:"model"`
	CreatedAt time.Time `json:"created_at"`
	ExpiresAt time.Time `json:"expires_at"`
	HitCount  int       `json:"hit_count"`
}

Entry is a single cached response stored on disk.

type Stats

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

Stats holds aggregate cache statistics.

Jump to

Keyboard shortcuts

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