Documentation
¶
Index ¶
- func Options() *options
- type Cache
- type LRUCache
- type TTLCache
- func (c *TTLCache[K]) Delete(key K)
- func (c *TTLCache[K]) ForEach(f func(key K, value any) error) error
- func (c *TTLCache[K]) ForceCleanup()
- func (c *TTLCache[K]) Get(key K) (any, bool)
- func (c *TTLCache[K]) Has(key K) bool
- func (c *TTLCache[K]) Set(key K, value any, opts *options) bool
- func (c *TTLCache[K]) Size() int
- type TTLCacheAttrs
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache[K comparable] interface { // Get retrieves a value from the cache. Get(key K) (any, bool) // Set adds a value to the cache, applying any provided options. Set(key K, value any, opts *options) bool Delete(key K) Has(key K) bool // ForEach iterates over the cache items. ForEach(f func(key K, value any) error) error // Size returns the number of items in the cache. Size() int }
Cache is the unified interface for all cache implementations. The Set method accepts a variadic list of options.
func NewLRUCache ¶
func NewLRUCache[K comparable]( capacity int, onInvalidate func(key K, value any), ) Cache[K]
NewLRUCache creates a new LRU Cache instance. Capacity must be greater than zero.
type LRUCache ¶
type LRUCache[K comparable] struct { // contains filtered or unexported fields }
LRUCache is a concurrent, fixed-size cache with an LRU eviction policy.
func (*LRUCache[K]) Delete ¶ added in v1.5.0
func (c *LRUCache[K]) Delete(key K)
Delete removes an item from the cache.
func (*LRUCache[K]) Get ¶ added in v1.5.0
Get retrieves a value from the cache. If found, the item is promoted to Most Recently Used (MRU).
type TTLCache ¶
type TTLCache[K comparable] struct { // contains filtered or unexported fields }
TTLCache is a high-performance, sharded, generic TTL cache.
func NewTTLCache ¶
func NewTTLCache[K comparable]( attrs TTLCacheAttrs, ) *TTLCache[K]
NewTTLCache creates a new sharded TTL cache with a background janitor goroutine. numShards specifies the number of shards to create and must be greater than 0. cleanupInterval specifies how often the janitor should run.
func (*TTLCache[K]) Delete ¶ added in v1.5.0
func (c *TTLCache[K]) Delete(key K)
Delete removes an item from the cache.
func (*TTLCache[K]) ForceCleanup ¶
func (c *TTLCache[K]) ForceCleanup()
ForceCleanup actively scans all shards and deletes expired items. This is called periodically by the janitor but can also be called manually.
func (*TTLCache[K]) Get ¶ added in v1.5.0
Get retrieves an item from the cache. It returns the item (of type T) and true if found and not expired. Otherwise, it returns the zero value of T and false.