Documentation ¶
Overview ¶
Package simplelru provices a non thread-safe LRU cache with maximum size and TTL
Index ¶
- type LRU
- func (m *LRU[K, T]) Add(key K, value T, size int, expire time.Time) bool
- func (m *LRU[K, T]) Available() int
- func (m *LRU[K, T]) Evict(key K)
- func (m *LRU[K, T]) EvictExpired() bool
- func (m *LRU[K, T]) ForEach(fn func(K, T, int, time.Time) bool)
- func (m *LRU[K, T]) Get(key K) (T, time.Time, bool)
- func (m *LRU[K, T]) Len() int
- func (m *LRU[K, T]) Size() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LRU ¶
type LRU[K comparable, T any] struct { // contains filtered or unexported fields }
LRU Implements a least-recently-used cache with a maximum size and optional expiration date
func NewLRU ¶
func NewLRU[K comparable, T any](size int, onAdd func(K, T, int, time.Time), onEvict func(K, T, int)) *LRU[K, T]
NewLRU creates a new LRU with maximum size and eviction callback
func (*LRU[K, T]) Add ¶
Add adds an entry of a given size and optional expiration date, and returns true if entries were removed
func (*LRU[K, T]) EvictExpired ¶
EvictExpired scans the whole cache and evicts all expired entries
func (*LRU[K, T]) ForEach ¶
ForEach allows you to iterate over all non-expired entries in the Cache. ForEach will automatically evict any expired entry it finds along the way, and it will stop if the callback returns true.
func (*LRU[K, T]) Get ¶
Get tries to find an entry, and returns its value, expiration date, and if it was found