package lru
import "gorm.io/gorm/internal/lru"
Index ¶
- type Entry
- type EvictCallback
- type LRU
- func NewLRU[K comparable, V any](size int, onEvict EvictCallback[K, V], ttl time.Duration) *LRU[K, V]
- func (c *LRU[K, V]) Add(key K, value V) (evicted bool)
- func (c *LRU[K, V]) Cap() int
- func (c *LRU[K, V]) Contains(key K) (ok bool)
- func (c *LRU[K, V]) Get(key K) (value V, ok bool)
- func (c *LRU[K, V]) GetOldest() (key K, value V, ok bool)
- func (c *LRU[K, V]) KeyValues() map[K]V
- func (c *LRU[K, V]) Keys() []K
- func (c *LRU[K, V]) Len() int
- func (c *LRU[K, V]) Peek(key K) (value V, ok bool)
- func (c *LRU[K, V]) Purge()
- func (c *LRU[K, V]) Remove(key K) bool
- func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool)
- func (c *LRU[K, V]) Resize(size int) (evicted int)
- func (c *LRU[K, V]) Values() []V
- type LruList
- func NewList[K comparable, V any]() *LruList[K, V]
- func (l *LruList[K, V]) Back() *Entry[K, V]
- func (l *LruList[K, V]) Init() *LruList[K, V]
- func (l *LruList[K, V]) Length() int
- func (l *LruList[K, V]) MoveToFront(e *Entry[K, V])
- func (l *LruList[K, V]) PushFront(k K, v V) *Entry[K, V]
- func (l *LruList[K, V]) PushFrontExpirable(k K, v V, expiresAt time.Time) *Entry[K, V]
- func (l *LruList[K, V]) Remove(e *Entry[K, V]) V
Types ¶
type Entry ¶
type Entry[K comparable, V any] struct { // The LRU Key of this element. Key K // The Value stored with this element. Value V // The time this element would be cleaned up, optional ExpiresAt time.Time // The expiry bucket item was put in, optional ExpireBucket uint8 // contains filtered or unexported fields }
Entry is an LRU Entry
func (*Entry[K, V]) PrevEntry ¶
PrevEntry returns the previous list element or nil.
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 LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU implements a thread-safe LRU with expirable entries.
func NewLRU ¶
func NewLRU[K comparable, V any](size int, onEvict EvictCallback[K, V], ttl time.Duration) *LRU[K, V]
NewLRU returns a new thread-safe cache with expirable entries.
Size parameter set to 0 makes cache of unlimited size, e.g. turns LRU mechanism off.
Providing 0 TTL turns expiring off.
Delete expired entries every 1/100th of ttl value. Goroutine which deletes expired entries runs indefinitely.
func (*LRU[K, V]) Add ¶
Add adds a value to the cache. Returns true if an eviction occurred. Returns false if there was no eviction: the item was already in the cache, or the size was not exceeded.
func (*LRU[K, V]) Cap ¶
Cap returns the capacity of the cache
func (*LRU[K, V]) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*LRU[K, V]) Get ¶
Get looks up a key's value from the cache.
func (*LRU[K, V]) GetOldest ¶
GetOldest returns the oldest entry
func (*LRU[K, V]) KeyValues ¶
func (c *LRU[K, V]) KeyValues() map[K]V
func (*LRU[K, V]) Keys ¶
func (c *LRU[K, V]) Keys() []K
Keys returns a slice of the keys in the cache, from oldest to newest. Expired entries are filtered out.
func (*LRU[K, V]) Len ¶
Len returns the number of items in the cache.
func (*LRU[K, V]) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*LRU[K, V]) Purge ¶
func (c *LRU[K, V]) Purge()
Purge clears the cache completely. onEvict is called for each evicted key.
func (*LRU[K, V]) Remove ¶
Remove removes the provided key from the cache, returning if the key was contained.
func (*LRU[K, V]) RemoveOldest ¶
RemoveOldest removes the oldest item from the cache.
func (*LRU[K, V]) Resize ¶
Resize changes the cache size. Size of 0 means unlimited.
func (*LRU[K, V]) Values ¶
func (c *LRU[K, V]) Values() []V
Values returns a slice of the values in the cache, from oldest to newest. Expired entries are filtered out.
type LruList ¶
type LruList[K comparable, V any] struct { // contains filtered or unexported fields }
LruList represents a doubly linked list. The zero Value for LruList is an empty list ready to use.
func NewList ¶
func NewList[K comparable, V any]() *LruList[K, V]
NewList returns an initialized list.
func (*LruList[K, V]) Back ¶
Back returns the last element of list l or nil if the list is empty.
func (*LruList[K, V]) Init ¶
Init initializes or clears list l.
func (*LruList[K, V]) Length ¶
Length returns the number of elements of list l. The complexity is O(1).
func (*LruList[K, V]) MoveToFront ¶
MoveToFront moves element e to the front of list l. If e is not an element of l, the list is not modified. The element must not be nil.
func (*LruList[K, V]) PushFront ¶
PushFront inserts a new element e with value v at the front of list l and returns e.
func (*LruList[K, V]) PushFrontExpirable ¶
PushFrontExpirable inserts a new expirable element e with Value v at the front of list l and returns e.
func (*LruList[K, V]) Remove ¶
Remove removes e from its list, decrements l.len
Source Files ¶
lru.go
- Version
- v1.26.1 (latest)
- Published
- May 7, 2025
- Platform
- linux/amd64
- Imports
- 2 packages
- Last checked
- 1 week ago –
Tools for package owners.