moonbitstack/moonkart/policy does not have a README file

    Policy

    pub(open) trait Policy {
    fn hit(self : Self, id : Int64, hash : Int) -> Unit
    fn admit(self : Self, id : Int64, weight : Int64, hash : Int) -> Array[Int64]
    fn forget(self : Self, id : Int64) -> Unit
    fn size(self : Self) -> Int
    }

    A pluggable eviction policy. The cache assigns every entry an opaque numeric id and hands the policy the entry's key hash alongside it. Policies that track frequency use the hash, so a key keeps its history across eviction and re-admission even though every insertion gets a fresh id. A policy never sees a key or a value. Implement this trait of your own type and hand the instance to the cache to use a strategy the library does not ship.

    IntrusiveList

    pub struct IntrusiveList {
    // private fields
    }

    A doubly linked list keyed by id, with 0 as the empty marker so valid ids (which start at 1) never collide with it. Every mutable piece lives in a Ref so that copying the list (for example passing it to a method that takes self by value) still mutates the one shared list.
    pub struct Link {
    // private fields
    }

    A node of the intrusive doubly linked list used by the LRU-family policies.

    Lru

    pub struct Lru {
    // private fields
    }

    Least-recently-used policy: the entry not touched for the longest time leaves first.
    impl Policy for Lru

    Lru::admit

    fn Lru::admit(self : Lru, id : Int64, weight : Int64, _hash : Int) -> Array[Int64]

    Lru::forget

    fn Lru::forget(self : Lru, id : Int64) -> Unit

    Lru::hit

    fn Lru::hit(self : Lru, id : Int64, _hash : Int) -> Unit

    Lru::size

    fn Lru::size(self : Lru) -> Int

    S3Fifo

    pub struct S3Fifo {
    // private fields
    }

    S3-FIFO-inspired second-chance FIFO: a single FIFO queue with a visited bit per entry. When the queue is over budget, an entry that has been read since it entered is moved back to the tail with its bit cleared, while an entry never read since it entered is evicted. Scan floods and one-hit wonders are evicted on first sight, while repeated keys survive several rounds, at the cost of one boolean per entry.
    impl Policy for S3Fifo

    S3Fifo::admit

    fn S3Fifo::admit(self : S3Fifo, id : Int64, weight : Int64, _hash : Int) -> Array[Int64]

    S3Fifo::forget

    fn S3Fifo::forget(self : S3Fifo, id : Int64) -> Unit

    S3Fifo::hit

    fn S3Fifo::hit(self : S3Fifo, id : Int64, _hash : Int) -> Unit

    S3Fifo::size

    fn S3Fifo::size(self : S3Fifo) -> Int

    Sketch

    pub struct Sketch {
    // private fields
    }

    A Count-Min frequency sketch with periodic aging. The doorkeeper in Tinylfu keeps one-hit wonders from polluting the counters.

    Tinylfu

    pub struct Tinylfu {
    // private fields
    }

    W-TinyLFU policy: a small admission window feeds a segmented LRU (probation and protected) that is guarded by a frequency sketch keyed by the entry's key hash, so often-read keys survive even after they age out of recency. This is the cache's default strategy.
    impl Policy for Tinylfu

    Tinylfu::admit

    fn Tinylfu::admit(self : Tinylfu, id : Int64, weight : Int64, hash : Int) -> Array[Int64]

    Tinylfu::forget

    fn Tinylfu::forget(self : Tinylfu, id : Int64) -> Unit

    Tinylfu::hit

    fn Tinylfu::hit(self : Tinylfu, id : Int64, hash : Int) -> Unit

    Tinylfu::size

    fn Tinylfu::size(self : Tinylfu) -> Int

    new_lru

    fn new_lru(cap? : Int64, max_weight? : Int64) -> Lru

    Creates an LRU policy. Set at most one of max_weight (evict by weight) or cap (evict by entry count).

    new_s3fifo

    fn new_s3fifo(cap? : Int64, max_weight? : Int64) -> S3Fifo

    Creates an S3-FIFO-inspired second-chance FIFO. Bound by cap (entry count) or max_weight (total weight).

    new_tinylfu

    fn new_tinylfu(cap? : Int64, max_weight? : Int64, window_ratio? : Double, protected_ratio? : Double) -> Tinylfu

    Creates a W-TinyLFU policy. window_ratio sizes the admission window; protected_ratio sizes the protected segment of the main region.

    tinylfu_protected_ratio

    let tinylfu_protected_ratio : Double

    Fraction of the main region given to the protected segment in W-TinyLFU. Entries read at least once are promoted here, away from the probation queue.

    tinylfu_window_ratio

    let tinylfu_window_ratio : Double

    Fraction of capacity given to the admission window in W-TinyLFU. The window admits new entries before they must earn a place in the main region, so a burst of one-hit wonders does not evict the working set.

    Source Files