cache

Generated by tinox doc

tinox.core.cache, bundled with the compiler and also available as a standalone tinox-central package.

Lru cache

import tinox.core.cache
import tinox.core.option
class Main
{
    fnc main() -> Int32
    {
        const cache: Cache<String, Int64> = Cache::new(2)
        Cache::set(cache, "a", 1)
        Cache::set(cache, "b", 2)
        Cache::set(cache, "c", 3) // evicts "a", the least-recently-used entry

        const found: Option<Int64> = Cache::get(cache, "a")
        println("has a: " + found.isSome().toString())
        const value: Option<Int64> = Cache::get(cache, "c")
        println("c: " + value.unwrap().toString())
        return 0
    }
}

class Cache<K, V>

Fields

dataMap<K, V>
maxSizeInt64
accessOrderList<K>

Methods

fn new(maxSize: Int64) → Cache<K, V>
fn get(cache: Cache<K, V>, key: K) → Option<V>
fn set(cache: Cache<K, V>, key: K, value: V) → Nothing
fn remove(cache: Cache<K, V>, key: K) → Bool
fn clear(cache: Cache<K, V>) → Nothing
fn touch(cache: Cache<K, V>, key: K) → Nothing

fn tinoxLcNew(cap: Int64, ttlMs: Int64) → Int64

fn tinoxLcBegin(h: Int64, key: String) → Int64

fn tinoxLcValue() → String

fn tinoxLcStore(h: Int64, key: String, value: String, token: Int64) → Nothing

fn tinoxLcAbort(h: Int64, key: String) → Nothing

fn tinoxLcEvictAll(h: Int64) → Nothing

fn tinoxLcEvict(h: Int64, key: String) → Nothing

fn tinoxLcStat(h: Int64, which: Int64) → Int64

class LoadingCache

* A bounded, thread-safe, in-process cache from String keys to String values (encode other * types yourself -- the serialization is explicit) with a time-to-live and single-flight loading: * * const cache = LoadingCache::create(500, 30000) // at most 500 entries, 30 s TTL * const price = cache.get("sku-7", key => loadPriceFromDb(key)) * * Concurrent identical misses run the loader ONCE; the other callers wait for that result (at most * TINOX_LC_WAIT_MS, default 60 s, after which a waiter loads on its own and `waitedOut()` counts it). * A loader that throws caches nothing: the error reaches its own caller, the waiters retry (one of * them becomes the next loader). The least recently used entry is dropped when full. evict()/ * evictAll() also invalidate loads in flight: such a load still returns its value to its caller but is * not stored, so a value read before an update never outlives the eviction that followed it. * Memory-only and per process: other instances keep their own copy, so use short TTLs for data * that other writers change.

Fields

handleInt64

Methods

fnc create(capacity: Int64, ttlMs: Int64) → LoadingCache
fn get(key: String, loader: fnc(String) -> String) → String
fn evict(key: String) → Nothing
Drops one key (and invalidates a load of it in flight)
fn evictAll() → Nothing
Drops everything (and invalidates all loads in flight)
fn hits() → Int64
fn misses() → Int64
Lookups that found nothing usable (each starts a load or joins one)
fn loads() → Int64
Loader runs that completed and returned a value
fn evictions() → Int64
Entries dropped by expiry, LRU pressure or eviction
fn failures() → Int64
Loader runs that threw
fn waitedOut() → Int64
Waiters that gave up waiting for the in-flight loader and loaded themselves
fn size() → Int64
Entries currently stored

class LruCache<K, V>

Fields

cacheCache<K, V>

Methods

fn new(maxSize: Int64) → LruCache<K, V>
fn get(cache: LruCache<K, V>, key: K) → Option<V>
fn set(cache: LruCache<K, V>, key: K, value: V) → Nothing