resourcecache

package
v1.5.0-rc.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package resourcecache provides a thread-safe LRU cache with memory-based eviction for caching parsed HCL resources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a thread-safe LRU cache with memory-based eviction

func New

func New(config CacheConfig) *Cache

New creates a new LRU cache with the given configuration

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all items from the cache

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes an item from the cache

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, bool)

Get retrieves an item from the cache. Returns the value and true if found, nil and false otherwise.

func (*Cache) Keys

func (c *Cache) Keys() []string

Keys returns all keys currently in the cache (for iteration/debugging)

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache

func (*Cache) Put

func (c *Cache) Put(key string, value interface{})

Put adds an item to the cache, evicting least recently used items if necessary

func (*Cache) Stats

func (c *Cache) Stats() CacheStats

Stats returns cache statistics

type CacheConfig

type CacheConfig struct {
	MaxMemoryBytes int64         // Maximum memory usage (default: 50MB)
	MaxEntries     int           // Maximum entries (0 = unlimited, use memory only)
	TTL            time.Duration // Time-to-live for entries (0 = no expiry)
}

CacheConfig configures the LRU cache

func DefaultConfig

func DefaultConfig() CacheConfig

DefaultConfig returns sensible defaults for the cache

type CacheStats

type CacheStats struct {
	Entries     int     // Number of entries in cache
	MemoryBytes int64   // Current memory usage
	MaxMemory   int64   // Maximum memory limit
	Hits        int64   // Number of cache hits
	Misses      int64   // Number of cache misses
	Evictions   int64   // Number of evicted entries
	HitRate     float64 // Hit rate (0.0 - 1.0)
}

CacheStats holds cache performance statistics

type MetricsCollector

type MetricsCollector struct {
	// contains filtered or unexported fields
}

MetricsCollector tracks cache performance over time

func NewMetricsCollector

func NewMetricsCollector(cache *Cache, interval time.Duration) *MetricsCollector

NewMetricsCollector creates a new metrics collector for the given cache

func (*MetricsCollector) Collect

func (m *MetricsCollector) Collect()

Collect takes a snapshot of current cache stats

func (*MetricsCollector) JSON

func (m *MetricsCollector) JSON() ([]byte, error)

JSON returns the current stats as JSON

func (*MetricsCollector) Report

func (m *MetricsCollector) Report() string

Report returns a human-readable report of cache statistics

func (*MetricsCollector) ResetStats

func (m *MetricsCollector) ResetStats()

ResetStats resets the hit/miss/eviction counters (for testing or periodic reset)

func (*MetricsCollector) Samples

func (m *MetricsCollector) Samples() []CacheStats

Samples returns all collected samples

func (*MetricsCollector) Start

func (m *MetricsCollector) Start()

Start begins periodic metrics collection

func (*MetricsCollector) Stop

func (m *MetricsCollector) Stop()

Stop stops periodic metrics collection

type ResourceCache

type ResourceCache struct {
	// contains filtered or unexported fields
}

ResourceCache specializes Cache for HCL resources with type-safe access methods

func NewResourceCache

func NewResourceCache(config CacheConfig) *ResourceCache

NewResourceCache creates a cache for parsed resources

func (*ResourceCache) Clear

func (rc *ResourceCache) Clear()

Clear clears the cache

func (*ResourceCache) Get

func (rc *ResourceCache) Get(name string) (interface{}, bool)

Get retrieves any cached value by key (generic access)

func (*ResourceCache) GetResource

func (rc *ResourceCache) GetResource(name string) (modconfig.HclResource, bool)

GetResource retrieves a parsed resource by full name

func (*ResourceCache) Invalidate

func (rc *ResourceCache) Invalidate(name string)

Invalidate removes a specific resource

func (*ResourceCache) InvalidateAll

func (rc *ResourceCache) InvalidateAll(predicate func(string) bool)

InvalidateAll removes all resources matching a predicate

func (*ResourceCache) Keys

func (rc *ResourceCache) Keys() []string

Keys returns all keys currently in the cache

func (*ResourceCache) Len

func (rc *ResourceCache) Len() int

Len returns the number of items in the cache

func (*ResourceCache) Put

func (rc *ResourceCache) Put(name string, value interface{})

Put caches any value by key (generic access)

func (*ResourceCache) PutResource

func (rc *ResourceCache) PutResource(name string, resource modconfig.HclResource)

PutResource caches a parsed resource

func (*ResourceCache) Stats

func (rc *ResourceCache) Stats() CacheStats

Stats returns cache statistics

type Sizer

type Sizer interface {
	Size() int64
}

Sizer interface for items that can report their memory size

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL