Documentation
¶
Overview ¶
Package cache provides LRU blob caching with Bloom pre-filter and cost-based eviction.
Index ¶
- Constants
- Variables
- func IsStale(meta IncrementalMeta, currentRootSHA string) bool
- func Key(rootSHA, branch string) string
- func WriteMeta(dir string, meta IncrementalMeta) error
- type IncrementalMeta
- type LRUBlobCache
- func (c *LRUBlobCache) CacheHits() int64
- func (c *LRUBlobCache) CacheMisses() int64
- func (c *LRUBlobCache) Clear()
- func (c *LRUBlobCache) Get(hash gitlib.Hash) *gitlib.CachedBlob
- func (c *LRUBlobCache) GetMulti(hashes []gitlib.Hash) (found map[gitlib.Hash]*gitlib.CachedBlob, missing []gitlib.Hash)
- func (c *LRUBlobCache) Put(hash gitlib.Hash, blob *gitlib.CachedBlob)
- func (c *LRUBlobCache) PutMulti(blobs map[gitlib.Hash]*gitlib.CachedBlob)
- func (c *LRUBlobCache) PutMultiOwned(blobs map[gitlib.Hash]*gitlib.CachedBlob)
- func (c *LRUBlobCache) Stats() LRUStats
- type LRUStats
Constants ¶
const DefaultLRUCacheSize = 256 * 1024 * 1024
DefaultLRUCacheSize is the default maximum memory size for the LRU blob cache (256 MB).
Variables ¶
var ErrCacheCorrupt = errors.New("cache metadata corrupt")
ErrCacheCorrupt is returned when the cache metadata file cannot be parsed.
var ErrCacheNotFound = errors.New("cache metadata not found")
ErrCacheNotFound is returned when the cache metadata file does not exist.
Functions ¶
func IsStale ¶
func IsStale(meta IncrementalMeta, currentRootSHA string) bool
IsStale returns true when the cached root SHA does not match the current root SHA, indicating a force-push or history rewrite.
func Key ¶
Key produces a deterministic directory name from root SHA and branch. The key is a SHA-256 hash of "rootSHA:branch", hex-encoded.
func WriteMeta ¶
func WriteMeta(dir string, meta IncrementalMeta) error
WriteMeta atomically writes cache metadata as indented JSON to dir/cache.json.
Types ¶
type IncrementalMeta ¶
type IncrementalMeta struct {
Version int `json:"version"`
HeadSHA string `json:"head_sha"`
Branch string `json:"branch"`
RootSHA string `json:"root_sha"`
CommitCount int `json:"commit_count"`
AnalyzerIDs []string `json:"analyzer_ids"`
Timestamp time.Time `json:"timestamp"`
}
IncrementalMeta holds metadata for an incremental analysis cache.
func ReadMeta ¶
func ReadMeta(dir string) (IncrementalMeta, error)
ReadMeta reads and parses cache metadata from dir/cache.json. Returns ErrCacheNotFound if the file does not exist. Returns ErrCacheCorrupt if the file cannot be parsed.
type LRUBlobCache ¶
type LRUBlobCache struct {
// contains filtered or unexported fields
}
LRUBlobCache provides a cross-commit LRU cache for blob data. It tracks memory usage and evicts least recently used entries when the limit is exceeded. A Bloom filter pre-filters Get/GetMulti lookups to skip lock acquisition for definite misses.
func NewLRUBlobCache ¶
func NewLRUBlobCache(maxSize int64) *LRUBlobCache
NewLRUBlobCache creates a new LRU blob cache with the specified maximum size in bytes. A Bloom filter is initialized to pre-filter lookups, sized for the estimated element count.
func (*LRUBlobCache) CacheHits ¶
func (c *LRUBlobCache) CacheHits() int64
CacheHits returns the total cache hit count (atomic, lock-free).
func (*LRUBlobCache) CacheMisses ¶
func (c *LRUBlobCache) CacheMisses() int64
CacheMisses returns the total cache miss count (atomic, lock-free).
func (*LRUBlobCache) Clear ¶
func (c *LRUBlobCache) Clear()
Clear removes all entries from the cache and resets the Bloom filter.
func (*LRUBlobCache) Get ¶
func (c *LRUBlobCache) Get(hash gitlib.Hash) *gitlib.CachedBlob
Get retrieves a blob from the cache. Returns nil if not found. Uses a Bloom filter to skip lock acquisition for definite cache misses.
func (*LRUBlobCache) GetMulti ¶
func (c *LRUBlobCache) GetMulti(hashes []gitlib.Hash) (found map[gitlib.Hash]*gitlib.CachedBlob, missing []gitlib.Hash)
GetMulti retrieves multiple blobs from the cache. Returns a map of found blobs and a slice of missing hashes.
func (*LRUBlobCache) Put ¶
func (c *LRUBlobCache) Put(hash gitlib.Hash, blob *gitlib.CachedBlob)
Put adds a blob to the cache. If the cache exceeds maxSize, entries are evicted using size-aware eviction (large, infrequently accessed items evicted first).
func (*LRUBlobCache) PutMulti ¶
func (c *LRUBlobCache) PutMulti(blobs map[gitlib.Hash]*gitlib.CachedBlob)
PutMulti adds multiple blobs to the cache.
func (*LRUBlobCache) PutMultiOwned ¶
func (c *LRUBlobCache) PutMultiOwned(blobs map[gitlib.Hash]*gitlib.CachedBlob)
PutMultiOwned adds multiple blobs without cloning. The caller guarantees the blobs are exclusively owned heap copies (not arena-backed).
func (*LRUBlobCache) Stats ¶
func (c *LRUBlobCache) Stats() LRUStats
Stats returns cache statistics.