lrucache

stdlib::rss::lrucache

A generic least-recently-used cache implemented in pure RustScript.

Import

use stdlib::rss::lrucache;

Data types

TypeDescription
LruCacheStateOpaque cache state passed to and returned by cache operations.
LruGetResultResult of a promoting cache lookup: the updated cache state and an optional payload.
LruEntryRowKey-value row returned by entries.

Data type details

LruCacheState

struct LruCacheState<K, V> {
    capacity: int,
    size: int,
    next_id: int,
    head: int,
    tail: int,
    lookup: map<int>,
    nodes: map<LruNode<K, V>>
}

Opaque cache state passed to and returned by cache operations.

LruGetResult

struct LruGetResult<K, V> {
    cache: LruCacheState<K, V>,
    value: Option<V>
}

Result of a promoting cache lookup: the updated cache state and an optional payload.

LruEntryRow

struct LruEntryRow<K, V> {
    key: K,
    value: V
}

Key-value row returned by entries.

Functions

FunctionDescription
newCreates an LRU cache with the given capacity.
capacityReturns the cache capacity.
lenReturns the number of cached entries.
is_emptyReturns whether the cache is empty.
hasReturns whether the cache contains a key.
peekReturns the cached value without updating recency, or None when the key is absent.
getPromotes the key to the most recently used position and returns the updated cache state with value: Option<V>; a missing key yields None and leaves the state unchanged.
putInserts or updates a cache entry and returns the next cache state.
touchPromotes a key to the most recently used position if it exists.
removeRemoves a cache entry by key.
clearRemoves all entries from the cache.
keysReturns cache keys from oldest to newest.
valuesReturns cache values from oldest to newest.
entriesReturns cache entries from oldest to newest.
oldestReturns the oldest cache entry, or None when the cache is empty.
newestReturns the newest cache entry, or None when the cache is empty.

Function details

new

pub fn new<K, V>(raw_capacity: int) -> LruCacheState<K, V>

Creates an LRU cache with the given capacity.

capacity

pub fn capacity<K, V>(cache: LruCacheState<K, V>) -> int

Returns the cache capacity.

len

pub fn len<K, V>(cache: LruCacheState<K, V>) -> int

Returns the number of cached entries.

is_empty

pub fn is_empty<K, V>(cache: LruCacheState<K, V>) -> bool

Returns whether the cache is empty.

has

pub fn has<K, V>(cache: LruCacheState<K, V>, key: K) -> bool

Returns whether the cache contains a key.

peek

pub fn peek<K, V>(cache: LruCacheState<K, V>, key: K) -> Option<V>

Returns the cached value without updating recency, or None when the key is absent.

get

pub fn get<K, V>(cache: LruCacheState<K, V>, key: K) -> LruGetResult<K, V>

Promotes the key to the most recently used position and returns the updated cache state with value: Option<V>; a missing key yields None and leaves the state unchanged.

put

pub fn put<K, V>(cache: LruCacheState<K, V>, key: K, value: V) -> LruCacheState<K, V>

Inserts or updates a cache entry and returns the next cache state.

touch

pub fn touch<K, V>(cache: LruCacheState<K, V>, key: K) -> LruCacheState<K, V>

Promotes a key to the most recently used position if it exists.

remove

pub fn remove<K, V>(cache: LruCacheState<K, V>, key: K) -> LruCacheState<K, V>

Removes a cache entry by key.

clear

pub fn clear<K, V>(cache: LruCacheState<K, V>) -> LruCacheState<K, V>

Removes all entries from the cache.

keys

pub fn keys<K, V>(cache: LruCacheState<K, V>) -> [K]

Returns cache keys from oldest to newest.

values

pub fn values<K, V>(cache: LruCacheState<K, V>) -> [V]

Returns cache values from oldest to newest.

entries

pub fn entries<K, V>(cache: LruCacheState<K, V>) -> [LruEntryRow<K, V>]

Returns cache entries from oldest to newest.

oldest

pub fn oldest<K, V>(cache: LruCacheState<K, V>) -> Option<LruEntryRow<K, V>>

Returns the oldest cache entry, or None when the cache is empty.

newest

pub fn newest<K, V>(cache: LruCacheState<K, V>) -> Option<LruEntryRow<K, V>>

Returns the newest cache entry, or None when the cache is empty.