tinylru

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 1 Imported by: 23

README

tinylru

GoDoc

A fast little LRU cache.

Getting Started

Installing

To start using tinylru, install Go and run go get:

$ go get -u github.com/tidwall/tinylru

This will retrieve the library.

Usage

Note: this package also has a non-generic tinylru.LRU implementation.

// Create an LRU cache
var cache tinylru.LRUG[string, string]

// Set the cache size. This is the maximum number of items that the cache can
// hold before evicting old items. The default size is 256.
cache.Resize(1024)

// Set a key. Returns the previous value and ok if a previous value exists.
prev, ok := cache.Set("hello", "world")

// Get a key. Returns the value and ok if the value exists.
value, ok := cache.Get("hello")

// Delete a key. Returns the deleted value and ok if a previous value exists.
prev, ok := cache.Delete("hello")

A Set function may evict old items when adding a new item while LRU is at capacity. If you want to know what was evicted then use the SetEvicted function.

// Set a key and return the evicted item, if any.
prev, ok, evictedKey, evictedValue, evicted := cache.SetEvicted("hello", "jello")
Contact

Josh Baker @tidwall

License

tinylru source code is available under the MIT License.

Documentation

Index

Constants

View Source
const DefaultSize = 256

DefaultSize is the default maximum size of an LRU cache before older items get automatically evicted.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

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

LRU implements an LRU cache

func (*LRU) Clear added in v1.2.1

func (lru *LRU) Clear()

Clear will remove all key/values from the LRU cache

func (*LRU) Contains added in v1.1.0

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

Contains returns true if the key exists.

func (*LRU) Delete

func (lru *LRU) Delete(key interface{}) (prev interface{}, deleted bool)

Delete a value for a key

func (*LRU) Get

func (lru *LRU) Get(key interface{}) (value interface{}, ok bool)

Get a value for key

func (*LRU) Len

func (lru *LRU) Len() int

Len returns the length of the lru cache

func (*LRU) Peek added in v1.1.0

func (lru *LRU) Peek(key interface{}) (value interface{}, ok bool)

Peek returns the value for key value without updating the recently used status.

func (*LRU) Range

func (lru *LRU) Range(iter func(key interface{}, value interface{}) bool)

Range iterates over all key/values in the order of most recently to least recently used items.

func (*LRU) Resize

func (lru *LRU) Resize(size int) (evictedKeys []interface{},
	evictedValues []interface{})

Resize sets the maximum size of an LRU cache. If this value is less than the number of items currently in the cache, then items will be evicted. Returns evicted items. This operation will panic if the size is less than one.

func (*LRU) Reverse

func (lru *LRU) Reverse(iter func(key interface{}, value interface{}) bool)

Reverse iterates over all key/values in the order of least recently to most recently used items.

func (*LRU) Set

func (lru *LRU) Set(key interface{}, value interface{}) (prev interface{},
	replaced bool)

Set or replace a value for a key.

func (*LRU) SetEvicted

func (lru *LRU) SetEvicted(key interface{}, value interface{}) (
	prev interface{}, replaced bool, evictedKey interface{},
	evictedValue interface{}, evicted bool)

SetEvicted sets or replaces a value for a key. If this operation causes an eviction then the evicted item is returned.

type LRUG added in v1.2.0

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

LRUG implements an LRU cache

func (*LRUG[Key, Value]) Clear added in v1.2.1

func (lru *LRUG[Key, Value]) Clear()

Clear will remove all key/values from the LRU cache

func (*LRUG[Key, Value]) Contains added in v1.2.0

func (lru *LRUG[Key, Value]) Contains(key Key) bool

Contains returns true if the key exists.

func (*LRUG[Key, Value]) Delete added in v1.2.0

func (lru *LRUG[Key, Value]) Delete(key Key) (prev Value, deleted bool)

Delete a value for a key

func (*LRUG[Key, Value]) Get added in v1.2.0

func (lru *LRUG[Key, Value]) Get(key Key) (value Value, ok bool)

Get a value for key

func (*LRUG[Key, Value]) Len added in v1.2.0

func (lru *LRUG[Key, Value]) Len() int

Len returns the length of the lru cache

func (*LRUG[Key, Value]) Peek added in v1.2.0

func (lru *LRUG[Key, Value]) Peek(key Key) (value Value, ok bool)

Peek returns the value for key value without updating the recently used status.

func (*LRUG[Key, Value]) Range added in v1.2.0

func (lru *LRUG[Key, Value]) Range(iter func(key Key, value Value) bool)

Range iterates over all key/values in the order of most recently to least recently used items.

func (*LRUG[Key, Value]) Resize added in v1.2.0

func (lru *LRUG[Key, Value]) Resize(size int) (evictedKeys []Key,
	evictedValues []Value)

Resize sets the maximum size of an LRU cache. If this value is less than the number of items currently in the cache, then items will be evicted. Returns evicted items. This operation will panic if the size is less than one.

func (*LRUG[Key, Value]) Reverse added in v1.2.0

func (lru *LRUG[Key, Value]) Reverse(iter func(key Key, value Value) bool)

Reverse iterates over all key/values in the order of least recently to most recently used items.

func (*LRUG[Key, Value]) Set added in v1.2.0

func (lru *LRUG[Key, Value]) Set(key Key, value Value) (prev Value,
	replaced bool)

Set or replace a value for a key.

func (*LRUG[Key, Value]) SetEvicted added in v1.2.0

func (lru *LRUG[Key, Value]) SetEvicted(key Key, value Value) (
	prev Value, replaced bool, evictedKey Key,
	evictedValue Value, evicted bool)

SetEvicted sets or replaces a value for a key. If this operation causes an eviction then the evicted item is returned.

Jump to

Keyboard shortcuts

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