Documentation ¶
Index ¶
- type EvictCallback
- type LruCache
- func (c *LruCache[K, V]) Clear() error
- func (c *LruCache[K, V]) CloneTo(n *LruCache[K, V])
- func (c *LruCache[K, V]) Compute(key K, valueFn func(oldValue V, loaded bool) (newValue V, delete bool)) (actual V, ok bool)
- func (c *LruCache[K, V]) Delete(key K)
- func (c *LruCache[K, V]) Exist(key K) bool
- func (c *LruCache[K, V]) Get(key K) (V, bool)
- func (c *LruCache[K, V]) GetOrStore(key K, constructor func() V) (V, bool)
- func (c *LruCache[K, V]) GetWithExpire(key K) (V, time.Time, bool)
- func (c *LruCache[K, V]) Set(key K, value V)
- func (c *LruCache[K, V]) SetWithExpire(key K, value V, expires time.Time)
- type Option
- func WithAge[K comparable, V any](maxAge int64) Option[K, V]
- func WithEvict[K comparable, V any](cb EvictCallback[K, V]) Option[K, V]
- func WithSize[K comparable, V any](maxSize int) Option[K, V]
- func WithStale[K comparable, V any](stale bool) Option[K, V]
- func WithUpdateAgeOnGet[K comparable, V any]() Option[K, V]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EvictCallback ¶
type EvictCallback[K comparable, V any] func(key K, value V)
EvictCallback is used to get a callback when a cache entry is evicted
type LruCache ¶
type LruCache[K comparable, V any] struct { // contains filtered or unexported fields }
LruCache is a thread-safe, in-memory lru-cache that evicts the least recently used entries from memory when (if set) the entries are older than maxAge (in seconds). Use the New constructor to create one.
func New ¶
func New[K comparable, V any](options ...Option[K, V]) *LruCache[K, V]
New creates an LruCache
func (*LruCache[K, V]) Compute ¶ added in v1.18.7
func (c *LruCache[K, V]) Compute( key K, valueFn func(oldValue V, loaded bool) (newValue V, delete bool), ) (actual V, ok bool)
Compute either sets the computed new value for the key or deletes the value for the key. When the delete result of the valueFn function is set to true, the value will be deleted, if it exists. When delete is set to false, the value is updated to the newValue. The ok result indicates whether value was computed and stored, thus, is present in the map. The actual result contains the new value in cases where the value was computed and stored.
func (*LruCache[K, V]) Delete ¶
func (c *LruCache[K, V]) Delete(key K)
Delete removes the value associated with a key.
func (*LruCache[K, V]) Exist ¶
Exist returns if key exist in cache but not put item to the head of linked list
func (*LruCache[K, V]) Get ¶
Get returns any representation of a cached response and a bool set to true if the key was found.
func (*LruCache[K, V]) GetOrStore ¶
func (*LruCache[K, V]) GetWithExpire ¶
GetWithExpire returns any representation of a cached response, a time.Time Give expected expires, and a bool set to true if the key was found. This method will NOT check the maxAge of element and will NOT update the expires.
func (*LruCache[K, V]) Set ¶
func (c *LruCache[K, V]) Set(key K, value V)
Set stores any representation of a response for a given key.
func (*LruCache[K, V]) SetWithExpire ¶
SetWithExpire stores any representation of a response for a given key and given expires. The expires time will round to second.
type Option ¶
type Option[K comparable, V any] func(*LruCache[K, V])
Option is part of Functional Options Pattern
func WithAge ¶
func WithAge[K comparable, V any](maxAge int64) Option[K, V]
WithAge defined element max age (second)
func WithEvict ¶
func WithEvict[K comparable, V any](cb EvictCallback[K, V]) Option[K, V]
WithEvict set the evict callback
func WithSize ¶
func WithSize[K comparable, V any](maxSize int) Option[K, V]
WithSize defined max length of LruCache
func WithStale ¶
func WithStale[K comparable, V any](stale bool) Option[K, V]
WithStale decide whether Stale return is enabled. If this feature is enabled, element will not get Evicted according to `WithAge`.
func WithUpdateAgeOnGet ¶
func WithUpdateAgeOnGet[K comparable, V any]() Option[K, V]
WithUpdateAgeOnGet update expires when Get element