simplelru

package module
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2024 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package simplelru provices a non thread-safe LRU cache with maximum size and TTL

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRU

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

LRU Implements a least-recently-used cache with a maximum size and optional expiration date

func NewLRU

func NewLRU[K comparable, T any](size int,
	onAdd func(K, T, int, time.Time),
	onEvict func(K, T, int)) *LRU[K, T]

NewLRU creates a new LRU with maximum size and eviction callback

func (*LRU[K, T]) Add

func (m *LRU[K, T]) Add(key K, value T, size int, expire time.Time) bool

Add adds an entry of a given size and optional expiration date, and returns true if entries were removed

func (*LRU[K, T]) Available

func (m *LRU[K, T]) Available() int

Available tells how much space is available without evictions

func (*LRU[K, T]) Evict

func (m *LRU[K, T]) Evict(key K)

Evict removes an entry if present

func (*LRU[K, T]) EvictExpired

func (m *LRU[K, T]) EvictExpired() bool

EvictExpired scans the whole cache and evicts all expired entries

func (*LRU[K, T]) ForEach

func (m *LRU[K, T]) ForEach(fn func(K, T, int, time.Time) bool)

ForEach allows you to iterate over all non-expired entries in the Cache. ForEach will automatically evict any expired entry it finds along the way, and it will stop if the callback returns true.

func (*LRU[K, T]) Get

func (m *LRU[K, T]) Get(key K) (T, time.Time, bool)

Get tries to find an entry, and returns its value, expiration date, and if it was found

func (*LRU[K, T]) Len

func (m *LRU[K, T]) Len() int

Len returns the number of entries in the cache

func (*LRU[K, T]) Size

func (m *LRU[K, T]) Size() int

Size returns the added size of all entries in the cache

Jump to

Keyboard shortcuts

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