ttl

    A small in-memory TTL cache for MoonBit

    ttl
    cache
    time-to-live
    in-memory
    Download zip
    Author
    Version
    0.1.2
    License
    MIT
    Last updated
    2 months ago
    Downloads
    42

    #justjavac/ttl

    CI coverage

    A small runtime-neutral TTL cache for MoonBit.

    ///|
    test {
    let ttl : @ttl.TTL[String] = @ttl.TTL::new(default_ttl_ms=10_000L)

    assert_true(ttl.set("foo", "bar", now_ms=0L))

    guard ttl.get("foo", now_ms=1L) is Some("bar") else {
    fail("expected foo hit")
    }
    guard ttl.get("foo", now_ms=10_000L) is None else {
    fail("expected foo to expire")
    }
    }

    CacheEntry

    pub(all) struct CacheEntry[T] {
    key : String
    val : T
    expire_ms : Int64
    } derive(Eq,
    Debug
    )

    Item returned by TTL::entries.

    EventKind

    pub(all) enum EventKind {
    Hit
    Miss
    Expired
    Drop
    Set
    Del
    } derive(Eq, Hash,
    Debug
    )

    Event kind emitted by a TTL cache.
    impl Show for EventKind

    Item

    type Item[T]

    Listener

    type Listener[T]

    SetEntry

    pub(all) struct SetEntry[T] {
    key : String
    val : T
    ttl_ms : Int64?
    } derive(Eq,
    Debug
    )

    Item accepted by TTL::mset.

    SetEntry::new

    fn[T] SetEntry::new(key : String, val : T, ttl_ms? : Int64) -> SetEntry[T]

    Creates an entry for TTL::mset.

    TTL

    pub struct TTL[T] {
    store : Map[String, Item[T]]
    listeners : Map[EventKind, Array[Listener[T]]]
    default_ttl_ms : Int64
    capacity : Int
    next_listener_id : Int
    }

    A generic in-memory time-to-live cache.

    The cache is runtime-neutral: callers pass now_ms to methods that need a clock. Expired values are removed lazily on reads or explicit cleanup.

    TTL::add_listener

    fn[T] TTL::add_listener(self : TTL[T], kind : EventKind, callback : (TTLEvent[T]) -> Unit) -> Int

    Registers an event listener and returns its listener id.

    TTL::capacity

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

    Returns the maximum number of keys accepted by the cache.

    TTL::clear

    fn[T] TTL::clear(self : TTL[T]) -> Unit

    Removes all keys, emitting Del for each removed item.

    TTL::default_ttl_ms

    fn[T] TTL::default_ttl_ms(self : TTL[T]) -> Int64

    Returns the configured default TTL in milliseconds.

    TTL::del

    fn[T] TTL::del(self : TTL[T], key : String) -> T?

    Deletes a key and returns its value when present.

    TTL::entries

    fn[T] TTL::entries(self : TTL[T], now_ms~ : Int64) -> Array[CacheEntry[T]]

    Returns unexpired cache entries. Expired keys are purged before the snapshot is produced.

    TTL::get

    fn[T] TTL::get(self : TTL[T], key : String, now_ms~ : Int64) -> T?

    Gets a value by key and removes it first if it has expired.

    TTL::has

    fn[T] TTL::has(self : TTL[T], key : String, now_ms~ : Int64) -> Bool

    Returns whether a key exists and has not expired.

    TTL::mset

    fn[T] TTL::mset(self : TTL[T], entries : ArrayView[SetEntry[T]], now_ms~ : Int64) -> Int

    Sets multiple keys and returns the number accepted by the cache.

    TTL::new

    fn[T] TTL::new(default_ttl_ms? : Int64, capacity? : Int) -> TTL[T]

    Creates an empty TTL cache.

    TTL::purge_expired

    fn[T] TTL::purge_expired(self : TTL[T], now_ms~ : Int64) -> Int

    Removes all expired keys and returns the number removed.

    TTL::remove_listener

    fn[T] TTL::remove_listener(self : TTL[T], kind : EventKind, listener_id : Int) -> Bool

    Removes a previously registered listener.

    TTL::set

    fn[T] TTL::set(self : TTL[T], key : String, val : T, now_ms~ : Int64, ttl_ms? : Int64) -> Bool

    Sets a key-value pair. Returns false when a new key is dropped because the cache is at capacity.

    TTL::set_capacity

    fn[T] TTL::set_capacity(self : TTL[T], capacity : Int) -> Bool

    Updates cache capacity. Negative values are rejected.

    TTL::set_default_ttl_ms

    fn[T] TTL::set_default_ttl_ms(self : TTL[T], ttl_ms : Int64) -> Unit

    Updates the default TTL in milliseconds.

    TTL::size

    fn[T] TTL::size(self : TTL[T], now_ms? : Int64) -> Int

    Returns the number of stored keys. When now_ms is supplied, expired keys are purged before counting.

    TTLEvent

    pub(all) struct TTLEvent[T] {
    kind : EventKind
    key : String
    val : T?
    expire_ms : Int64?
    } derive(Eq,
    Debug
    )

    Event payload passed to cache listeners.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io