simplelru

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2022 License: MPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package simplelru provides simple LRU implementation based on build-in container/list.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EvictCallback

type EvictCallback[Key, Value any] func(key Key, value Value)

EvictCallback is used to get a callback when a cache entry is evicted

type LRU

type LRU[Key comparable, Value any] struct {
	// contains filtered or unexported fields
}

LRU implements a non-thread safe fixed size LRU cache

func NewLRU

func NewLRU[Key comparable, Value any](size int, onEvict EvictCallback[Key, Value]) (*LRU[Key, Value], error)

NewLRU constructs an LRU of the given size

func (*LRU[Key, Value]) Add

func (c *LRU[Key, Value]) Add(key Key, value Value) (evicted bool)

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

func (*LRU[Key, Value]) Contains

func (c *LRU[Key, Value]) Contains(key Key) (ok bool)

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

func (*LRU[Key, Value]) Get

func (c *LRU[Key, Value]) Get(key Key) (value Value, ok bool)

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

func (*LRU[Key, Value]) GetOldest

func (c *LRU[Key, Value]) GetOldest() (key Key, value Value, ok bool)

GetOldest returns the oldest entry

func (*LRU[Key, Value]) Keys

func (c *LRU[Key, Value]) Keys() []Key

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

func (*LRU[Key, Value]) Len

func (c *LRU[Key, Value]) Len() int

Len returns the number of items in the cache.

func (*LRU[Key, Value]) Peek

func (c *LRU[Key, Value]) Peek(key Key) (value Value, ok bool)

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

func (*LRU[Key, Value]) Purge

func (c *LRU[Key, Value]) Purge()

Purge is used to completely clear the cache.

func (*LRU[Key, Value]) Remove

func (c *LRU[Key, Value]) Remove(key Key) (present bool)

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

func (*LRU[Key, Value]) RemoveOldest

func (c *LRU[Key, Value]) RemoveOldest() (key Key, value Value, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*LRU[Key, Value]) Resize

func (c *LRU[Key, Value]) Resize(size int) (evicted int)

Resize changes the cache size.

type LRUCache

type LRUCache[Key, Value any] interface {
	// Adds a value to the cache, returns true if an eviction occurred and
	// updates the "recently used"-ness of the key.
	Add(key Key, value Value) bool

	// Returns key's value from the cache and
	// updates the "recently used"-ness of the key. #value, isFound
	Get(key Key) (value Value, ok bool)

	// Checks if a key exists in cache without updating the recent-ness.
	Contains(key Key) (ok bool)

	// Returns key's value without updating the "recently used"-ness of the key.
	Peek(key Key) (value Value, ok bool)

	// Removes a key from the cache.
	Remove(key Key) bool

	// Removes the oldest entry from cache.
	RemoveOldest() (Key, Value, bool)

	// Returns the oldest entry from the cache. #key, value, isFound
	GetOldest() (Key, Value, bool)

	// Returns a slice of the keys in the cache, from oldest to newest.
	Keys() []Key

	// Returns the number of items in the cache.
	Len() int

	// Clears all cache entries.
	Purge()

	// Resizes cache, returning number evicted
	Resize(int) int
}

LRUCache is the interface for simple LRU cache.

Jump to

Keyboard shortcuts

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