lru

package
v0.4.43 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultEvictedBufferSize defines the default buffer size to store evicted key/val
	DefaultEvictedBufferSize = 16
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache is a thread-safe fixed size LRU cache.

func New

func New[K comparable, V any](size int) (*Cache[K, V], error)

New creates an LRU of the given size.

func NewWithEvict

func NewWithEvict[K comparable, V any](size int, onEvicted func(key K, value V)) (c *Cache[K, V], err error)

NewWithEvict constructs a fixed size cache with the given eviction callback.

func (*Cache[K, V]) Add

func (c *Cache[K, V]) Add(key K, value V) (evicted bool)

Add adds a value to the cache. Returns true if an eviction occurred.

func (*Cache[K, V]) Contains

func (c *Cache[K, V]) Contains(key K) bool

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*Cache[K, V]) ContainsOrAdd

func (c *Cache[K, V]) ContainsOrAdd(key K, value V) (ok, evicted bool)

ContainsOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.

func (*Cache[K, V]) ContainsOrNew

func (c *Cache[K, V]) ContainsOrNew(key K, constructor func() V) (ok, evicted bool)

ContainsOrNew checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, use constructor create the value and add. Returns whether found and whether an eviction occurred.

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (value V, ok bool)

Get looks up a key's value from the cache.

func (*Cache[K, V]) GetOldest

func (c *Cache[K, V]) GetOldest() (key, value interface{}, ok bool)

GetOldest returns the oldest entry

func (*Cache[K, V]) GetOrAdd

func (c *Cache[K, V]) GetOrAdd(key K, value V) (previous V, ok, evicted bool)

GetOrAdd checks if a key is in the cache and if not, adds the value. Returns whether found and whether an eviction occurred.

func (*Cache[K, V]) GetOrNew

func (c *Cache[K, V]) GetOrNew(key K, constructor func() V) (value V, ok, evicted bool)

GetOrNew checks if a key is in the cache and if not, use constructor create the value and add. Returns whether found and whether an eviction occurred.

func (*Cache[K, V]) Items added in v0.4.12

func (c *Cache[K, V]) Items() []simplelru.Entry[K, V]

Items returns a slice of the keys and values in the cache, from oldest to newest.

func (*Cache[K, V]) Keys

func (c *Cache[K, V]) Keys() []K

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

Len returns the number of items in the cache.

func (*Cache[K, V]) Peek

func (c *Cache[K, V]) Peek(key K) (value V, ok bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

func (*Cache[K, V]) PeekOrAdd

func (c *Cache[K, V]) PeekOrAdd(key K, value V) (previous V, ok, evicted bool)

PeekOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.

func (*Cache[K, V]) PeekOrNew

func (c *Cache[K, V]) PeekOrNew(key K, constructor func() V) (value V, ok, evicted bool)

PeekOrNew checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, use constructor create the value and add. Returns whether found and whether an eviction occurred.

func (*Cache[K, V]) Purge

func (c *Cache[K, V]) Purge()

Purge is used to completely clear the cache.

func (*Cache[K, V]) Remove

func (c *Cache[K, V]) Remove(key K) (present bool)

Remove removes the provided key from the cache.

func (*Cache[K, V]) RemoveOldest

func (c *Cache[K, V]) RemoveOldest() (key, value interface{}, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*Cache[K, V]) Resize

func (c *Cache[K, V]) Resize(size int) (evicted int)

Resize changes the cache size.

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 ExpirableCache

type ExpirableCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ExpirableCache implements a thread safe LRU with expirable entries.

func NewExpirable

func NewExpirable[K comparable, V any](size int, onEvict EvictCallback[K, V], defaultTtl, purgeEvery time.Duration) *ExpirableCache[K, V]

NewExpirable returns a new cache with expirable entries.

If size <= 0 means unlimited size If defaultTtl <= 0 means by default there is no ttl If purgeEvery = 0 means 5min

Please remember to call .Close if you don't use the cache anymore

func (*ExpirableCache[K, V]) Add

func (c *ExpirableCache[K, V]) Add(key K, value V) (evicted bool)

Add adds a key and a value to the LRU interface (ttl equals to the default)

func (*ExpirableCache[K, V]) AddWithExpire

func (c *ExpirableCache[K, V]) AddWithExpire(key K, value V, expiresAt time.Time) (evicted bool)

AddWithExpire adds a key and a value with specific expire time to the LRU interface

func (*ExpirableCache[K, V]) AddWithTTL

func (c *ExpirableCache[K, V]) AddWithTTL(key K, value V, ttl time.Duration) (evicted bool)

AddWithTTL adds a key and a value with a TTL to the LRU interface

func (*ExpirableCache[K, V]) Close

func (c *ExpirableCache[K, V]) Close()

Close cleans the cache and destroys running goroutines

func (*ExpirableCache[K, V]) Contains

func (c *ExpirableCache[K, V]) Contains(key K) (ok bool)

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*ExpirableCache[K, V]) DeleteExpired

func (c *ExpirableCache[K, V]) DeleteExpired()

DeleteExpired clears cache of expired items

func (*ExpirableCache[K, V]) Get

func (c *ExpirableCache[K, V]) Get(key K) (V, bool)

Get returns the key value

func (*ExpirableCache[K, V]) GetOldest

func (c *ExpirableCache[K, V]) GetOldest() (key K, value V, ok bool)

GetOldest returns the oldest entry and mark it as recently-used

func (*ExpirableCache[K, V]) Keys

func (c *ExpirableCache[K, V]) Keys() []K

Keys returns a slice of the keys in the cache, from oldest to newest. Warning: returned slice may contain expired keys, if you want only un-expired keys, use UnexpiredKeys

func (*ExpirableCache[K, V]) Len

func (c *ExpirableCache[K, V]) Len() int

Len return count of items in cache

func (*ExpirableCache[K, V]) Peek

func (c *ExpirableCache[K, V]) Peek(key K) (V, bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

func (*ExpirableCache[K, V]) PeekOldest

func (c *ExpirableCache[K, V]) PeekOldest() (key K, value V, ok bool)

PeekOldest returns the oldest entry This function won't change the order of the entry, if you want to move it the front, use GetOldest

func (*ExpirableCache[K, V]) Purge

func (c *ExpirableCache[K, V]) Purge()

Purge clears the cache completely.

func (*ExpirableCache[K, V]) Remove

func (c *ExpirableCache[K, V]) Remove(key K) bool

Remove key from the cache

func (*ExpirableCache[K, V]) RemoveOldest

func (c *ExpirableCache[K, V]) RemoveOldest() (key K, value V, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*ExpirableCache[K, V]) Resize

func (c *ExpirableCache[K, V]) Resize(size int) (evicted int)

Resize changes the cache size. size 0 doesn't resize the cache, as it means unlimited.

func (*ExpirableCache[K, V]) UnexpiredKeys

func (c *ExpirableCache[K, V]) UnexpiredKeys() []K

UnexpiredKeys returns a slice of the keys in the cache, from oldest to newest.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL