
cache
Package cache provides an object data cache layer that sits between the orchestrator and storage backends, reducing API calls and egress by serving repeated reads from local storage.
Index
- type Entry
- type EntryMeta
- type MemoryCache
- func NewMemoryCache(cfg MemoryConfig) (*MemoryCache, error)
- func (c *MemoryCache) Admit(size int64) bool
- func (c *MemoryCache) Clear() int
- func (c *MemoryCache) Get(key string) (*Entry, bool)
- func (c *MemoryCache) Invalidate(key string)
- func (c *MemoryCache) InvalidatePrefix(prefix string) int
- func (c *MemoryCache) PutBytes(key string, data []byte, meta EntryMeta)
- func (c *MemoryCache) Stats() Stats
- type MemoryConfig
- type ObjectCache
- type Stats
type Entry
Entry holds a cached object’s data and metadata.
func (*Entry) Size
Size returns the approximate memory footprint of this entry in bytes.
type EntryMeta
EntryMeta holds the metadata to store alongside cached object data.
type MemoryCache
MemoryCache is a size-aware LRU cache backed by an in-memory map and doubly-linked list. Evicts least-recently-used entries when the total size exceeds MaxSize. Entries expire after TTL.
func NewMemoryCache
NewMemoryCache creates a new in-memory LRU cache with the given configuration.
func (*MemoryCache) Admit
Admit reports whether an entry whose data is approximately size bytes would be accepted by the cache. Compares against max_object_size only; total-cache capacity is handled by LRU eviction at PutBytes time. The “approximately” caveat exists because an Entry’s stored size also includes ContentType + ETag + Metadata key/value bytes, which the caller does not know until PutBytes runs - in practice these are tens of bytes versus the body size and never matter for admission.
func (*MemoryCache) Clear
Clear removes every entry and zeroes the LRU. Updates gauges so the cache_size_bytes / cache_entries dashboards reflect the flush immediately, without waiting for the next Get/Put.
func (*MemoryCache) Get
Get returns the cached entry if present and not expired. On hit, the entry is promoted to the front of the LRU list.
func (*MemoryCache) Invalidate
Invalidate removes a single key from the cache.
func (*MemoryCache) InvalidatePrefix
InvalidatePrefix removes every entry whose key starts with prefix and returns the number of entries dropped. Walks the items map (O(n)) so callers should expect cost proportional to cache size, not match count. Holds c.mu for the duration to keep map iteration safe.
func (*MemoryCache) PutBytes
PutBytes stores pre-buffered data in the cache. Callers must have called Admit first to confirm the size fits the per-entry limit; this method does not re-check that condition on the body bytes. Falls back to a silent no-op if even after evicting every existing entry the new entry cannot fit (e.g. metadata pushed it over MaxSize).
func (*MemoryCache) Stats
Stats returns current cache utilization.
type MemoryConfig
MemoryConfig holds tuning parameters for the in-memory cache.
type ObjectCache
ObjectCache caches object data to avoid repeated backend fetches. Implementations must be safe for concurrent use by multiple goroutines.
The interface separates admission decision from buffering so callers can refuse to read oversized payloads into memory in the first place: check Admit(size), and only buffer + PutBytes when admitted.
type Stats
Stats reports current cache utilization.
Generated by gomarkdoc