lru

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

README

lru

示例

package main

import (
	"fmt"

	"github.com/eachain/lru"
)

func main() {
	cache := lru.New[string, int](3)
	cache.Set("banana", 3)
	cache.Set("apple", 2)
	cache.Set("pear", 4)
	cache.Set("orange", 1)

	cache.All()(func(s string, i int) bool {
		fmt.Printf("%v:%v ", s, i)
		return true
	})
	// Output:
	// orange:1 pear:4 apple:2
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

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

LRU is a fixed size LRU cache.

func New

func New[K comparable, V any](size int) *LRU[K, V]

New creates a new LRU cache. If size is zero, the LRU has no limit and it's assumed that eviction is done by the caller.

func (*LRU[K, V]) All

func (lru *LRU[K, V]) All() func(yield func(K, V) bool)

Backward returns an iterator over key-value pairs in the lru cache, traversing it from the newest item.

func (*LRU[K, V]) Backward

func (lru *LRU[K, V]) Backward() func(yield func(K, V) bool)

Backward returns an iterator over key-value pairs in the lru cache, traversing it from the oldest item.

func (*LRU[K, V]) Clear

func (lru *LRU[K, V]) Clear()

Clear purges all stored items from the lru cache.

func (*LRU[K, V]) Get

func (lru *LRU[K, V]) Get(key K) (value V, ok bool)

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

func (*LRU[K, V]) Len

func (lru *LRU[K, V]) Len() int

Len returns the number of items in the lru cache.

func (*LRU[K, V]) OnEvicted

func (lru *LRU[K, V]) OnEvicted(cb func(K, V))

OnEvicted optionally specifies a callback function to be executed when an entry is purged from the lru cache.

func (*LRU[K, V]) Pick

func (lru *LRU[K, V]) Pick(key K) (value V, ok bool)

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

func (*LRU[K, V]) Remove

func (lru *LRU[K, V]) Remove(key K) (value V, ok bool)

Remove removes the provided key from the cache.

func (*LRU[K, V]) RemoveOldest

func (lru *LRU[K, V]) RemoveOldest() (key K, value V, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*LRU[K, V]) Resize

func (lru *LRU[K, V]) Resize(size int) (evicted int)

Resize changes the lru cache size.

func (*LRU[K, V]) Set

func (lru *LRU[K, V]) Set(key K, value V)

Set sets a value to the lru cache.

type MutexLRU

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

MutexLRU is a thread-safe fixed size LRU cache.

func NewWithMutex

func NewWithMutex[K comparable, V any](size int) *MutexLRU[K, V]

NewWithMutex creates a new thread-safe LRU cache. If size is zero, the LRU has no limit and it's assumed that eviction is done by the caller.

func (*MutexLRU[K, V]) All

func (m *MutexLRU[K, V]) All() func(yield func(K, V) bool)

Backward returns an iterator over key-value pairs in the lru cache, traversing it from the newest item.

func (*MutexLRU[K, V]) Backward

func (m *MutexLRU[K, V]) Backward() func(yield func(K, V) bool)

Backward returns an iterator over key-value pairs in the lru cache, traversing it from the oldest item.

func (*MutexLRU[K, V]) Clear

func (m *MutexLRU[K, V]) Clear()

Clear purges all stored items from the lru cache.

func (*MutexLRU[K, V]) Get

func (m *MutexLRU[K, V]) Get(key K) (value V, ok bool)

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

func (*MutexLRU[K, V]) Len

func (m *MutexLRU[K, V]) Len() int

Len returns the number of items in the lru cache.

func (*MutexLRU[K, V]) OnEvicted

func (m *MutexLRU[K, V]) OnEvicted(cb func(K, V))

OnEvicted optionally specifies a callback function to be executed when an entry is purged from the lru cache.

func (*MutexLRU[K, V]) Pick

func (m *MutexLRU[K, V]) Pick(key K) (value V, ok bool)

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

func (*MutexLRU[K, V]) Remove

func (m *MutexLRU[K, V]) Remove(key K) (value V, ok bool)

Remove removes the provided key from the lru cache.

func (*MutexLRU[K, V]) RemoveOldest

func (m *MutexLRU[K, V]) RemoveOldest() (key K, value V, ok bool)

RemoveOldest removes the oldest item from the cache.

func (*MutexLRU[K, V]) Resize

func (m *MutexLRU[K, V]) Resize(size int) (evicted int)

Resize changes the lru cache size.

func (*MutexLRU[K, V]) Set

func (m *MutexLRU[K, V]) Set(key K, value V)

Set sets a value to the lru cache.

Jump to

Keyboard shortcuts

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