Documentation
¶
Overview ¶
Package threadsafecache is a small wrapper around hashicorp/golang-lru that adds a per-entry TTL on top of LRU eviction.
The cache is generic over the value type; keys are strings. Reads use a read-lock when the entry is fresh and an upgraded write-lock only when the entry has expired and must be evicted, so the common hit path stays concurrent.
Zero-cost generics: the inner LRU is parameterised on the same type, so the cache stores values directly (not via interface{}).
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidSize = errors.New("threadsafecache: size must be > 0")
ErrInvalidSize is returned by New when size <= 0.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
Cache is a thread-safe size+TTL bounded cache.
A TTL of 0 disables expiry — entries live until they are evicted by the LRU policy or removed explicitly.
func New ¶
New returns a cache with the given LRU capacity and per-entry TTL. A non-positive size returns ErrInvalidSize; a non-positive ttl disables expiry.
func (*Cache[T]) Get ¶
Get returns the value for key. The boolean is false when the key is missing OR has expired (in the latter case the entry is also evicted as a side effect — touching expired keys cleans them up lazily).
func (*Cache[T]) Len ¶
Len returns the number of entries currently in the cache, including any that have expired but not yet been evicted.