moonbitstack/moonzero/collection does not have a README file

    Cache

    pub struct Cache[K, V] {
    limit : Int
    ttl_ms : Int64
    clock :
    Clock

    entries : Map[K, Entry[V]]
    links : Map[K, Link[K]]
    head : K?
    tail : K?
    }

    A cache with a size limit and an optional expiry: reads move an entry to the front, and the least recently used entry is the one evicted when the limit is reached.

    The recency order is a doubly linked list threaded through links, so a hit costs a constant number of map operations whatever the cache holds. An entry whose time is up is dropped on the next read rather than by a background sweep — a cache here is a performance device, not a store, so there is nothing to reclaim on a timer.

    Cache::contains

    fn[K : Hash + Eq, V] Cache::contains(self : Cache[K, V], key : K) -> Bool

    Whether a valid value is under key, without changing the recency order.

    Cache::get

    fn[K : Hash + Eq, V] Cache::get(self : Cache[K, V], key : K) -> V?

    The value under key if it is there and still valid. Reading moves the entry to the front of the recency list; an expired one is dropped.

    Cache::keys

    fn[K : Hash + Eq, V] Cache::keys(self : Cache[K, V]) -> Array[K]

    The keys in recency order, most recent first, expired ones excluded.

    Cache::len

    fn[K, V] Cache::len(self : Cache[K, V]) -> Int

    How many entries are held, expired ones included until they are read.

    Cache::limit

    fn[K, V] Cache::limit(self : Cache[K, V]) -> Int

    The configured limit.

    Cache::new

    fn[K : Hash + Eq, V] Cache::new(limit? : Int, ttl_ms? : Int64, clock? :
    Clock
    ) -> Cache[K, V]

    A cache holding at most limit entries, each valid for ttl_ms milliseconds (0 for no expiry), reading time from clock.

    Parameters

    • limit : entries kept before the least recently used is evicted
    • ttl_ms : how long an entry stays valid, 0 for forever
    • clock : the time source an expiry is measured against

    Cache::remove

    fn[K : Hash + Eq, V] Cache::remove(self : Cache[K, V], key : K) -> Unit

    Remove key from both the entries and the recency list.

    Cache::set

    fn[K : Hash + Eq, V] Cache::set(self : Cache[K, V], key : K, value : V) -> Unit

    Store value under key, evicting the least recently used entry when the cache is full. Storing an existing key refreshes its value, its expiry and its place in the recency list.

    Cache::set_until

    fn[K : Hash + Eq, V] Cache::set_until(self : Cache[K, V], key : K, value : V, ttl_ms : Int64) -> Unit

    Store value under key with its own lifetime in milliseconds; 0 means it never expires.

    Entry

    type Entry[V]

    One cached value and when it stops being valid; expires_at is 0 for an entry that never expires.
    type Link[K]

    One key's place in the recency list.

    Ring

    pub struct Ring[T] {
    slots : Array[T?]
    capacity : Int
    start : Int
    size : Int
    }

    A fixed-size circular buffer: pushing past the capacity overwrites the oldest sample. What a service keeps its last N events, timings or failures in.

    Ring::capacity

    fn[T] Ring::capacity(self : Ring[T]) -> Int

    The capacity it was built with.

    Ring::get

    fn[T] Ring::get(self : Ring[T], i : Int) -> T?

    The sample i places from the oldest, or None past the end.

    Ring::last

    fn[T] Ring::last(self : Ring[T]) -> T?

    The newest sample.

    Ring::len

    fn[T] Ring::len(self : Ring[T]) -> Int

    How many samples are held.

    Ring::new

    fn[T] Ring::new(capacity : Int) -> Ring[T]

    A ring holding at most capacity items.

    Parameters

    • capacity : how many items it keeps; 0 is raised to 1, because a ring of nothing could never hold a sample

    Ring::push

    fn[T] Ring::push(self : Ring[T], item : T) -> Unit

    Append a sample, overwriting the oldest one when the ring is full.

    Ring::to_array

    fn[T] Ring::to_array(self : Ring[T]) -> Array[T]

    The samples, oldest first.

    Source Files