libatframe_utils_memory

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Copyright 2025 atframework

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LRUMap

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

LRUMap is a generic LRU (Least Recently Used) cache with map-like access. K must be comparable (used as map key), V can be any type.

func NewLRUMap

func NewLRUMap[K comparable, V any](capacity int) *LRUMap[K, V]

NewLRUMap creates a new LRUMap with the specified capacity. If capacity <= 0, the cache will have unlimited capacity.

func (*LRUMap[K, V]) Back

func (c *LRUMap[K, V]) Back() (K, V, bool)

Back returns the least recently used key-value pair without removing it. Returns the zero values and false if the cache is empty.

func (*LRUMap[K, V]) Cap

func (c *LRUMap[K, V]) Cap() int

Cap returns the capacity of the cache.

func (*LRUMap[K, V]) Clear

func (c *LRUMap[K, V]) Clear()

Clear removes all items from the cache.

func (*LRUMap[K, V]) Contains

func (c *LRUMap[K, V]) Contains(key K) bool

Contains checks if a key exists in the cache without updating its access time.

func (*LRUMap[K, V]) Delete

func (c *LRUMap[K, V]) Delete(key K) bool

Delete removes a key from the cache. Returns true if the key was found and removed, false otherwise.

func (*LRUMap[K, V]) Front

func (c *LRUMap[K, V]) Front() (K, V, bool)

Front returns the most recently used key-value pair without removing it. Returns the zero values and false if the cache is empty.

func (*LRUMap[K, V]) Get

func (c *LRUMap[K, V]) Get(key K, updateVisit bool) (V, bool)

Get retrieves a value from the cache by key. If updateVisit is true, the item is moved to the front (most recently used). Returns the zero value and false if the key is not found.

func (*LRUMap[K, V]) Keys

func (c *LRUMap[K, V]) Keys() []K

Keys returns all keys in the cache, ordered from most to least recently used.

func (*LRUMap[K, V]) Len

func (c *LRUMap[K, V]) Len() int

Len returns the number of items in the cache.

func (*LRUMap[K, V]) PopBack

func (c *LRUMap[K, V]) PopBack() (K, V, bool)

PopBack removes and returns the least recently used key-value pair. Returns the zero values and false if the cache is empty.

func (*LRUMap[K, V]) PopFront

func (c *LRUMap[K, V]) PopFront() (K, V, bool)

PopFront removes and returns the most recently used key-value pair. Returns the zero values and false if the cache is empty.

func (*LRUMap[K, V]) Put

func (c *LRUMap[K, V]) Put(key K, value V)

Put adds or updates a key-value pair in the cache. If the key already exists, its value is updated and moved to the front. If the cache is at capacity, the least recently used item is evicted.

func (*LRUMap[K, V]) Range

func (c *LRUMap[K, V]) Range(fn func(key K, value V) bool)

Range iterates over all key-value pairs in the cache, from most to least recently used. If the callback returns false, iteration stops.

func (*LRUMap[K, V]) RangeReverse

func (c *LRUMap[K, V]) RangeReverse(fn func(key K, value V) bool)

RangeReverse iterates over all key-value pairs in the cache, from least to most recently used. If the callback returns false, iteration stops.

func (*LRUMap[K, V]) Resize

func (c *LRUMap[K, V]) Resize(newCapacity int)

Resize changes the capacity of the cache. If the new capacity is smaller than the current size, least recently used items are evicted. If newCapacity <= 0, the cache will have unlimited capacity.

func (*LRUMap[K, V]) Values

func (c *LRUMap[K, V]) Values() []V

Values returns all values in the cache, ordered from most to least recently used.

Jump to

Keyboard shortcuts

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