lrucache
stdlib::rss::lrucache
A generic least-recently-used cache implemented in pure RustScript.
Import
use stdlib::rss::lrucache;
Data types
| Type | Description |
|---|---|
LruCacheState | Opaque cache state passed to and returned by cache operations. |
LruPeekResult | Result of a non-promoting cache lookup. |
LruGetResult | Result of a promoting cache lookup, including the updated cache state. |
LruEntryReport | Optional oldest or newest cache entry. |
LruEntryRow | Key-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
| Function | Description |
|---|---|
new | Creates an LRU cache with the given capacity. |
capacity | Returns the cache capacity. |
len | Returns the number of cached entries. |
is_empty | Returns whether the cache is empty. |
has | Returns whether the cache contains a key. |
peek | Returns the cached value without updating recency. |
get | Returns the cached value and promotes it to the most recently used position. |
put | Inserts or updates a cache entry and returns the next cache state. |
touch | Promotes a key to the most recently used position if it exists. |
remove | Removes a cache entry by key. |
clear | Removes all entries from the cache. |
keys | Returns cache keys from oldest to newest. |
values | Returns cache values from oldest to newest. |
entries | Returns cache entries from oldest to newest. |
oldest | Returns the oldest cache entry report. |
newest | Returns 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.