Documentation
¶
Overview ¶
Package cache provides an in-memory LRU bounded by total byte size, item count, and entry age. Age eviction is passive: expired entries are removed on access (Get) and during bounded incremental sweeps on Put. No background goroutines are used.
This package is internal to cfsread and not intended for direct use outside the module.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// MaxBytes is the maximum total decompressed bytes across all entries.
// When exceeded, the least-recently-used entry is evicted.
MaxBytes int64
// MaxItems caps the number of cached entries.
MaxItems int
// MaxAge is the maximum idle duration before an entry becomes eligible
// for eviction. Expired entries are reclaimed lazily during Get and Put.
MaxAge time.Duration
// Now returns the current time. nil defaults to time.Now. Override in
// tests for deterministic time advancement with synctest.
Now func() time.Time
// OnEvict is called (under the LRU mutex) whenever an entry is removed
// for any reason except overwrite. nil means no callback.
OnEvict func(reason EvictionReason)
}
Config controls the behaviour of the LRU cache. Zero-value fields mean that dimension is unlimited.
type EvictionReason ¶
type EvictionReason int
EvictionReason indicates why an entry was removed from the cache.
const ( // EvictionSize indicates an entry was removed to stay under MaxBytes. EvictionSize EvictionReason = iota // EvictionCount indicates an entry was removed to stay under MaxItems. EvictionCount // EvictionIdle indicates an entry exceeded MaxAge. EvictionIdle // EvictionManual indicates an entry was explicitly invalidated. EvictionManual )
type LRU ¶
type LRU struct {
// contains filtered or unexported fields
}
LRU is an in-memory least-recently-used cache bounded by byte size, item count, and idle entry age.
func New ¶
New creates a new LRU cache using the supplied configuration. The internal map is preallocated with capacity matching MaxItems (or 64 if unlimited).
func (*LRU) Get ¶
Get retrieves the value for key. Returns the cached byte slice and true on hit, or nil, false on miss. Zero allocation on cache hit — the returned slice is shared with the cache and must not be mutated. If the entry has exceeded MaxAge, it is evicted and a miss is returned.
func (*LRU) Invalidate ¶
Invalidate removes a single entry by key. Fires OnEvict(EvictionManual) if the entry exists. Does nothing if the key is not present.
func (*LRU) InvalidatePrefix ¶
InvalidatePrefix removes all entries whose Key.FSName matches fsName. Fires OnEvict(EvictionManual) for each removed entry.
func (*LRU) Put ¶
Put inserts or replaces a value in the cache, then evicts as needed. If the key already exists, the old entry is silently replaced without triggering OnEvict. After insertion, the cache evicts in this order:
- Lazy sweep of expired entries (if MaxAge is set)
- Count-based eviction of the least-recently-used tail
- Byte-size-based eviction of the least-recently-used tail