lru

package
v0.2202.6 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package lru implements an in-memory Least-Recently-Used cache.

Index

Constants

This section is empty.

Variables

View Source
var ErrTooLarge = errors.New("lru: value size exceeds maximum capacity")

ErrTooLarge is the error returned when a value is too large for the cache.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Cache is an LRU cache instance.

func New

func New(options ...Option) (*Cache, error)

New creates a new LRU cache instance with the specified options.

func (*Cache) Clear added in v0.2011.0

func (c *Cache) Clear()

Clear empties the cache.

func (*Cache) Get

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

Get returns the value associated with the key and true if it is present in the cache, and the entry is moved to the most-recently-used position.

func (*Cache) Keys

func (c *Cache) Keys() []interface{}

Keys returns the keys for every entry in the cache, from the least-recently-used to the most-recently-used.

func (*Cache) Peek

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

Peek returns the value associated with the key and true if it is present in the cache, without altering the access time of the entry.

func (*Cache) Put

func (c *Cache) Put(key, value interface{}) error

Put inserts the key/value pair into the cache. If the key is already present, the value is updated, and the entry is moved to the most-recently-used position.

func (*Cache) Remove

func (c *Cache) Remove(key interface{}) bool

Remove removes the key from the cache and returns true if the key existed, otherwise false.

func (*Cache) Size

func (c *Cache) Size() uint64

Size returns the current cache size in the units specified by a `Capacity` option at creation time.

type OnEvictFunc

type OnEvictFunc func(key, value interface{})

OnEvictFunc is the function signature for the on-evict callback.

Note: The callback does not support calling routines on it's associated cache instance.

type Option

type Option func(c *Cache) error

Option is a configuration option used when instantiating a cache.

func Capacity

func Capacity(capacity uint64, inBytes bool) Option

Capacity sets the capacity of the new cache. If the capacity is set as `inBytes`, it is assumed that all values inserted will implement `Sizable`.

If no capacity is specified, the cache will have an unlimited size.

func OnEvict

func OnEvict(fn OnEvictFunc) Option

OnEvict sets the on-evict callback.

type Sizeable

type Sizeable interface {
	// Size returns the size of the instance in bytes.
	Size() uint64
}

Sizeable is the interface implemented by types that support returning their own memory size in bytes.

Jump to

Keyboard shortcuts

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