Documentation
¶
Overview ¶
Package cache provides persistent query caching for tok. Caches filtered command outputs for instant retrieval on repeated commands.
Index ¶
- func GenerateKey(command string, args []string, workingDir string, fileHashes map[string]string, ...) string
- func GetChangedFiles(workingDir string, oldCommit, newCommit string) ([]string, error)
- func IsGitRepo(workingDir string) bool
- type Cache
- type CacheEntry
- type CacheKeyOptions
- type CacheStats
- type FingerprintCache
- type GitWatcher
- type LRUCache
- type MultiLevelCache
- type QueryCache
- func (c *QueryCache) Cleanup(maxAge time.Duration) error
- func (c *QueryCache) Close() error
- func (c *QueryCache) Get(key string) (*CacheEntry, bool)
- func (c *QueryCache) GetRuntimeStats() (hits, misses int64)
- func (c *QueryCache) GetTopQueries(limit int) ([]*CacheEntry, error)
- func (c *QueryCache) Invalidate(predicate func(*CacheEntry) bool) error
- func (c *QueryCache) InvalidateByCommand(command string) error
- func (c *QueryCache) InvalidateByPrefix(prefix string) error
- func (c *QueryCache) Set(key string, command string, args []string, workingDir string, ...) error
- func (c *QueryCache) Stats() (*CacheStats, error)
- type RepoState
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 ¶
GetChangedFiles returns files changed between two commits
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) 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 ¶
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
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