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.
LruPeekResultResult of a non-promoting cache lookup.
LruGetResultResult of a promoting cache lookup, including the updated cache state.
LruEntryReportOptional oldest or newest cache entry.
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.

LruPeekResult

struct LruPeekResult<V> {
    found: bool,
    value: V
}

Result of a non-promoting cache lookup.

LruGetResult

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

Result of a promoting cache lookup, including the updated cache state.

LruEntryReport

struct LruEntryReport<K, V> {
    found: bool,
    key: K,
    value: V
}

Optional oldest or newest cache entry.

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.
getReturns the cached value and promotes it to the most recently used position.
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 report.
newestReturns the newest cache entry report.

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) -> LruPeekResult<V>

Returns the cached value without updating recency.

get

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

Returns the cached value and promotes it to the most recently used position.

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>) -> LruEntryReport<K, V>

Returns the oldest cache entry report.

newest

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

Returns the newest cache entry report.