Documentation
¶
Overview ¶
Package cache provides a thread-safe, TTL-bounded subgraph result cache keyed by Merkle subgraph roots.
When the hierarchical tree shows a package root has not changed, cached results for queries scoped to that package are still valid. The daemon invalidates entries after each index run by comparing old and new hierarchical trees and evicting entries for changed packages.
Index ¶
- Constants
- type CacheStats
- type SubgraphCache
- func (c *SubgraphCache) Clear()
- func (c *SubgraphCache) Get(key types.Hash) ([]byte, bool)
- func (c *SubgraphCache) Invalidate(key types.Hash)
- func (c *SubgraphCache) InvalidatePackages(packages []string, tree *snapshot.HierarchicalTree)
- func (c *SubgraphCache) Put(key types.Hash, value []byte)
- func (c *SubgraphCache) Stats() CacheStats
- type SubgraphCacheOptions
Constants ¶
const ( // DefaultMaxEntries is the default maximum number of cache entries. DefaultMaxEntries = 10_000 // DefaultTTL is the default time-to-live for cache entries. DefaultTTL = 1 * time.Hour )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheStats ¶
CacheStats holds hit/miss counters and size information for a SubgraphCache.
type SubgraphCache ¶
type SubgraphCache struct {
// contains filtered or unexported fields
}
SubgraphCache caches query results keyed by Merkle subgraph roots. When the hierarchical tree shows a package root has not changed, cached results for queries scoped to that package are still valid.
The cache is fully thread-safe via a sync.RWMutex. Reads use a shared lock; writes (Put, Invalidate, Clear) use an exclusive lock.
func NewSubgraphCache ¶
func NewSubgraphCache(opts SubgraphCacheOptions) *SubgraphCache
NewSubgraphCache creates a SubgraphCache with the given options. Zero-value options use the package-level defaults.
func (*SubgraphCache) Clear ¶
func (c *SubgraphCache) Clear()
Clear removes all entries from the cache.
func (*SubgraphCache) Get ¶
func (c *SubgraphCache) Get(key types.Hash) ([]byte, bool)
Get returns the cached value for key if it exists and has not expired. Returns (nil, false) on miss or expiry.
func (*SubgraphCache) Invalidate ¶
func (c *SubgraphCache) Invalidate(key types.Hash)
Invalidate removes the entry for key if it exists.
func (*SubgraphCache) InvalidatePackages ¶
func (c *SubgraphCache) InvalidatePackages(packages []string, tree *snapshot.HierarchicalTree)
InvalidatePackages removes cache entries for a set of changed packages. For each changed package, it looks up the package's root hash in tree and evicts any entry stored under that key. This is the daemon invalidation path: after each index run, call this with the list of packages that changed and the new hierarchical tree.
func (*SubgraphCache) Put ¶
func (c *SubgraphCache) Put(key types.Hash, value []byte)
Put stores value under key, overwriting any existing entry. When the cache is at capacity, a random existing entry is evicted to make room. The eviction strategy is intentionally simple: a random entry from the map is removed, which is O(1) and avoids the overhead of an LRU list for this use case.
func (*SubgraphCache) Stats ¶
func (c *SubgraphCache) Stats() CacheStats
Stats returns a snapshot of cache metrics. The returned struct is a value copy and is safe to read without holding any lock.
type SubgraphCacheOptions ¶
type SubgraphCacheOptions struct {
// MaxEntries is the maximum number of entries to hold before eviction.
// Defaults to DefaultMaxEntries when zero.
MaxEntries int
// TTL is the time-to-live for each entry.
// Defaults to DefaultTTL when zero.
TTL time.Duration
}
SubgraphCacheOptions configures a SubgraphCache.