cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package cache provides persistent query caching for tok. Caches filtered command outputs for instant retrieval on repeated commands.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateKey

func GenerateKey(command string, args []string, workingDir string, fileHashes map[string]string, opts ...CacheKeyOptions) string

GenerateKey creates a cache key from command context. IMPORTANT: budget, tier, and query intent are included in the hash so that queries with identical files but different filtering parameters never collide.

func GetChangedFiles

func GetChangedFiles(workingDir string, oldCommit, newCommit string) ([]string, error)

GetChangedFiles returns files changed between two commits

func IsGitRepo

func IsGitRepo(workingDir string) bool

IsGitRepo checks if a directory is a git repository

Types

type Cache

type Cache interface {
	Get(key string) (interface{}, bool)
	Set(key string, value interface{})
	Delete(key string)
	Len() int
}

Cache interface defines a generic cache

type CacheEntry

type CacheEntry struct {
	Key              string    `json:"key"`
	Command          string    `json:"command"`
	Args             string    `json:"args"`
	WorkingDir       string    `json:"working_dir"`
	FileHashes       string    `json:"file_hashes"`
	FilteredOutput   string    `json:"filtered_output"`
	OriginalTokens   int       `json:"original_tokens"`
	FilteredTokens   int       `json:"filtered_tokens"`
	CompressionRatio float64   `json:"compression_ratio"`
	CreatedAt        time.Time `json:"created_at"`
	AccessedAt       time.Time `json:"accessed_at"`
	HitCount         int       `json:"hit_count"`
}

CacheEntry represents a cached query result

type CacheKeyOptions

type CacheKeyOptions struct {
	Budget      int    // token budget; different budgets produce different filtered outputs
	Tier        string // processing tier (e.g. "minimal", "standard", "aggressive")
	QueryIntent string // the user's query intent / context
}

CacheKeyOptions holds optional context that must be included in the cache key to avoid returning wrong cached results for different budget/tier/intent contexts.

type CacheStats

type CacheStats struct {
	TotalEntries int64   `json:"total_entries"`
	TotalHits    int64   `json:"total_hits"`
	TotalMisses  int64   `json:"total_misses"`
	HitRate      float64 `json:"hit_rate"`
	TotalSaved   int64   `json:"total_tokens_saved"`
}

CacheStats holds cache statistics

type FingerprintCache

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

func GetGlobalCache

func GetGlobalCache() *FingerprintCache

func NewFingerprintCache

func NewFingerprintCache() *FingerprintCache

func (*FingerprintCache) Get

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

func (*FingerprintCache) Set

func (c *FingerprintCache) Set(key, value string)

type GitWatcher

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

GitWatcher monitors git state for cache invalidation

func NewGitWatcher

func NewGitWatcher(cache *QueryCache) *GitWatcher

NewGitWatcher creates a new git watcher

func (*GitWatcher) GetFileHashes

func (w *GitWatcher) GetFileHashes(workingDir string, files []string) (map[string]string, error)

GetFileHashes returns hashes for files in a directory Used to generate cache keys and detect changes

func (*GitWatcher) InvalidateChanged

func (w *GitWatcher) InvalidateChanged(workingDir string) error

InvalidateChanged invalidates cache entries for changed files

type LRUCache

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

LRUCache implements a true LRU cache with O(1) operations

func NewLRUCache

func NewLRUCache(capacity int, ttl time.Duration) *LRUCache

func (*LRUCache) Delete

func (c *LRUCache) Delete(key string)

func (*LRUCache) Get

func (c *LRUCache) Get(key string) (interface{}, bool)

func (*LRUCache) Len

func (c *LRUCache) Len() int

func (*LRUCache) Set

func (c *LRUCache) Set(key string, value interface{})

type MultiLevelCache

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

MultiLevelCache implements L1 (memory) + L2 (disk) + L3 (optional Redis) caching

func NewMultiLevelCache

func NewMultiLevelCache(l2Dir string, maxL1Size int) (*MultiLevelCache, error)

NewMultiLevelCache creates a 3-tier cache

func (*MultiLevelCache) Get

func (mc *MultiLevelCache) Get(key string) (string, bool)

Get retrieves from L1 → L2 → L3

func (*MultiLevelCache) Set

func (mc *MultiLevelCache) Set(key, value string)

Set stores in all cache levels

type QueryCache

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

QueryCache provides persistent caching of filtered outputs

func NewQueryCache

func NewQueryCache(dbPath string) (*QueryCache, error)

NewQueryCache creates a new query cache

func (*QueryCache) Cleanup

func (c *QueryCache) Cleanup(maxAge time.Duration) error

Cleanup removes old entries

func (*QueryCache) Close

func (c *QueryCache) Close() error

Close drains pending background writes and closes the cache database. Callers MUST call Close() when they are done using the QueryCache to release the underlying database connection and complete any pending writes. Failing to call Close() will leak file descriptors and potentially corrupt the WAL. Consider using defer qc.Close() immediately after NewQueryCache().

func (*QueryCache) Get

func (c *QueryCache) Get(key string) (*CacheEntry, bool)

Get retrieves a cached entry

func (*QueryCache) GetRuntimeStats

func (c *QueryCache) GetRuntimeStats() (hits, misses int64)

GetRuntimeStats returns runtime hit/miss stats

func (*QueryCache) GetTopQueries

func (c *QueryCache) GetTopQueries(limit int) ([]*CacheEntry, error)

GetTopQueries returns most frequently accessed queries

func (*QueryCache) Invalidate

func (c *QueryCache) Invalidate(predicate func(*CacheEntry) bool) error

Invalidate removes entries matching a predicate

func (*QueryCache) InvalidateByCommand

func (c *QueryCache) InvalidateByCommand(command string) error

InvalidateByCommand removes all entries for a command

func (*QueryCache) InvalidateByPrefix

func (c *QueryCache) InvalidateByPrefix(prefix string) error

InvalidateByPrefix removes entries with working dir prefix

func (*QueryCache) Set

func (c *QueryCache) Set(key string, command string, args []string, workingDir string,
	fileHashes map[string]string, filteredOutput string,
	originalTokens, filteredTokens int) error

Set stores a new cache entry

func (*QueryCache) Stats

func (c *QueryCache) Stats() (*CacheStats, error)

Stats returns cache statistics

type RepoState

type RepoState struct {
	WorkingDir  string
	HeadCommit  string
	DirtyFiles  map[string]string // file -> hash
	LastChecked time.Time
}

RepoState tracks the state of a git repository

Jump to

Keyboard shortcuts

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