Categorygithub.com/segmentio/agecache
modulepackage
0.1.3
Repository: https://github.com/segmentio/agecache.git
Documentation: pkg.go.dev

# README

agecache

Thread-safe LRU cache supporting expiration and jitter. Supports cache statistics, as well as eviction and expiration callbacks. Differs from some implementations in that OnEviction is only invoked when an entry is removed as a result of the LRU eviction policy - not when you explicitly delete it or when it expires. OnExpiration is available and invoked when an item expires. Expiration can be passively enforced when performing a Get, or actively enforced by iterating over all keys with an interval.

cache := agecache.New(agecache.Config{
	Capacity: 100,
	MaxAge:   70 * time.Minute,
	MinAge:   60 * time.Minute,
	OnExpiration: func(key, value interface{}) {
		// Handle expiration
	},
	OnEviction: func(key, value interface{}) {
		// Handle eviction
	},
})

cache.Set("foo", "bar")

Documentation

Full docs are available on Godoc.

# Functions

New constructs an LRU Cache with the given Config object.

# Constants

ActiveExpiration expires items by managing a goroutine to actively GC expired items in the background.
PassiveExpration expires items passively by checking the item expiry when `.Get()` is called, if the item was expired, it is deleted and nil is returned.

# Structs

Cache implements a thread-safe fixed-capacity LRU cache.
Config configures the cache.
Stats hold cache statistics.

# Interfaces

RandGenerator represents a random number generator.

# Type aliases

ExpirationType enumerates expiration types.