cache

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCacheDisabled indicates caching is disabled
	ErrCacheDisabled = errors.New("caching is disabled")
	// ErrNotFound indicates the entry was not found in cache
	ErrNotFound = errors.New("cache entry not found")
)

Functions

This section is empty.

Types

type Cache

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

Cache manages LLM response caching in SQLite

func New

func New(config Config) (*Cache, error)

New creates a new cache instance

func (*Cache) Clear

func (c *Cache) Clear() error

Clear removes all cache entries

func (*Cache) Close

func (c *Cache) Close() error

Close closes the database connection

func (*Cache) Get

func (c *Cache) Get(prompt, model string) (*Entry, error)

Get retrieves a cached response

func (*Cache) GetStats

func (c *Cache) GetStats() ([]StatsEntry, error)

GetStats retrieves aggregated statistics

func (Cache) GetTTL

func (c Cache) GetTTL() time.Duration

func (*Cache) Set

func (c *Cache) Set(entry *Entry) error

Set stores a response in the cache

type Config

type Config struct {
	DBPath  string        // Database file path (default: ~/.cache/commons-llm.db)
	TTL     time.Duration // Cache time-to-live
	NoCache bool          // Disable caching
	Debug   bool          // Enable debug output
}

Config holds cache configuration

type Entry

type Entry struct {
	ID               int64
	CacheKey         string
	PromptHash       string
	Model            string
	Prompt           string
	Response         string
	Error            string
	TokensInput      int
	TokensOutput     int
	TokensReasoning  int
	TokensCacheRead  int
	TokensCacheWrite int
	TokensTotal      int
	CostUSD          float64
	DurationMS       int64
	Provider         string
	Temperature      float64
	MaxTokens        int
	CreatedAt        time.Time
	AccessedAt       time.Time
	ExpiresAt        *time.Time
}

Entry represents a cached LLM response

type StatsEntry

type StatsEntry struct {
	Model                 string
	Provider              string
	TotalRequests         int64
	CacheHits             int64
	CacheMisses           int64
	ErrorCount            int64
	TotalInputTokens      int64
	TotalOutputTokens     int64
	TotalReasoningTokens  int64
	TotalCacheReadTokens  int64
	TotalCacheWriteTokens int64
	TotalCost             float64
	AvgDurationMS         int64
	FirstRequest          time.Time
	LastRequest           time.Time
}

StatsEntry represents aggregated statistics

Jump to

Keyboard shortcuts

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