lru

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLRU

func NewLRU(config *Config) (interfaces.LRU, error)

NewLRU constructs an LRU based on the specified config.

Types

type Config

type Config struct {
	// Function to calculate size of cache entries.
	SizeFn SizeFn
	// Optional callback for cache eviction events.
	OnEvict EvictedCallback
	// Maximum amount of data to store in the cache.
	// The size of each entry is determined by SizeFn.
	MaxSize int64
}

Config specifies how the LRU cache is to be constructed. MaxSize & SizeFn are required.

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry is used to hold a value in the evictList

type EvictedCallback

type EvictedCallback func(value interface{})

EvictedCallback is used to get a callback when a cache Entry is evicted

type LRU

type LRU struct {
	// contains filtered or unexported fields
}

LRU implements a non-thread safe fixed size LRU cache

func (*LRU) Add

func (c *LRU) Add(key, value interface{}) bool

Add adds a value to the cache. Returns true if the key was added.

func (*LRU) Contains

func (c *LRU) Contains(key interface{}) bool

Contains checks if a key is in the cache.

func (*LRU) Get

func (c *LRU) Get(key interface{}) (interface{}, bool)

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

func (*LRU) Len

func (c *LRU) Len() int

Len returns the number of items in the cache.

func (*LRU) MaxSize

func (c *LRU) MaxSize() int64

func (*LRU) Metrics

func (c *LRU) Metrics() string

func (*LRU) Peek

func (c *LRU) Peek(key interface{}) (interface{}, bool)

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

func (*LRU) Purge

func (c *LRU) Purge()

Purge is used to completely clear the cache.

func (*LRU) PushBack

func (c *LRU) PushBack(key, value interface{}) bool

PushBack adds a value to the back of the cache. Returns true if the key was added.

func (*LRU) Remove

func (c *LRU) Remove(key interface{}) (present bool)

Remove removes the provided key from the cache, returning if the key was contained.

func (*LRU) RemoveOldest

func (c *LRU) RemoveOldest() (interface{}, bool)

RemoveOldest removes the oldest item from the cache.

func (*LRU) Size

func (c *LRU) Size() int64

type SizeFn

type SizeFn func(value interface{}) int64

Jump to

Keyboard shortcuts

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