lru

package
v1.13.2 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: GPL-3.0 Imports: 2 Imported by: 333

Documentation

Overview

Package lru implements generically-typed LRU caches.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasicLRU

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

BasicLRU is a simple LRU cache.

This type is not safe for concurrent use. The zero value is not valid, instances must be created using NewCache.

func NewBasicLRU

func NewBasicLRU[K comparable, V any](capacity int) BasicLRU[K, V]

NewBasicLRU creates a new LRU cache.

func (*BasicLRU[K, V]) Add

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

Add adds a value to the cache. Returns true if an item was evicted to store the new item.

func (*BasicLRU[K, V]) Contains

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

Contains reports whether the given key exists in the cache.

func (*BasicLRU[K, V]) Get

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

Get retrieves a value from the cache. This marks the key as recently used.

func (*BasicLRU[K, V]) GetOldest

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

GetOldest retrieves the least-recently-used item. Note that this does not update the item's recency.

func (*BasicLRU[K, V]) Keys

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

Keys returns all keys in the cache.

func (*BasicLRU[K, V]) Len

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

Len returns the current number of items in the cache.

func (*BasicLRU[K, V]) Peek

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

Peek retrieves a value from the cache, but does not mark the key as recently used.

func (*BasicLRU[K, V]) Purge

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

Purge empties the cache.

func (*BasicLRU[K, V]) Remove

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

Remove drops an item from the cache. Returns true if the key was present in cache.

func (*BasicLRU[K, V]) RemoveOldest

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

RemoveOldest drops the least recently used item.

type Cache

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

Cache is a LRU cache. This type is safe for concurrent use.

func NewCache

func NewCache[K comparable, V any](capacity int) *Cache[K, V]

NewCache creates an LRU cache.

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 item was evicted to store the new item.

func (*Cache[K, V]) Contains

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

Contains reports whether the given key exists in the cache.

func (*Cache[K, V]) Get

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

Get retrieves a value from the cache. This marks the key as recently used.

func (*Cache[K, V]) Keys

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

Keys returns all keys of items currently in the LRU.

func (*Cache[K, V]) Len

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

Len returns the current number of items in the cache.

func (*Cache[K, V]) Peek

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

Peek retrieves a value from the cache, but does not mark the key as recently used.

func (*Cache[K, V]) Purge

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

Purge empties the cache.

func (*Cache[K, V]) Remove

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

Remove drops an item from the cache. Returns true if the key was present in cache.

type SizeConstrainedCache

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

SizeConstrainedCache is a cache where capacity is in bytes (instead of item count). When the cache is at capacity, and a new item is added, older items are evicted until the size constraint is met.

OBS: This cache assumes that items are content-addressed: keys are unique per content. In other words: two Add(..) with the same key K, will always have the same value V.

func NewSizeConstrainedCache

func NewSizeConstrainedCache[K comparable, V blobType](maxSize uint64) *SizeConstrainedCache[K, V]

NewSizeConstrainedCache creates a new size-constrained LRU cache.

func (*SizeConstrainedCache[K, V]) Add

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

Add adds a value to the cache. Returns true if an eviction occurred. OBS: This cache assumes that items are content-addressed: keys are unique per content. In other words: two Add(..) with the same key K, will always have the same value V. OBS: The value is _not_ copied on Add, so the caller must not modify it afterwards.

func (*SizeConstrainedCache[K, V]) Get

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

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

Jump to

Keyboard shortcuts

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