Micro Moka
A fast, lightweight, single-threaded cache for Rust. Micro Moka is forked from Mini Moka and intentionally omits async support, synchronization, weighted eviction, and expiration. It provides a bounded cache using the SIEVE eviction policy.
use Cache;
let mut cache = new;
cache.insert;
assert_eq!;
Why Micro Moka?
Efficient eviction. SIEVE gives accessed entries a second chance using one visited bit per entry and a persistent hand. Cache hits do not reorder a recency list, keeping the read path small.
Minimal overhead. The only production dependency is hashbrown. There is no
async runtime, lock, atomic, custom allocator, or unsafe code in Micro Moka.
Single-threaded by design. If a cache belongs to one thread, such as in CLI tools, WASM applications, game loops, compilers, or single-threaded servers, it does not need to pay synchronization costs.
Installation
[]
= "1"
Usage
use Cache;
let mut cache: = builder
.max_capacity
.initial_capacity
.build;
cache.insert;
assert_eq!;
assert!;
// Inspect without affecting SIEVE eviction state.
assert_eq!;
// Compute a missing value once.
assert_eq!;
cache.invalidate;
let removed = cache.remove;
assert_eq!;
cache.insert;
cache.insert;
cache.invalidate_entries_if;
for in cache.iter
cache.invalidate_all;
assert_eq!;
Custom Hashers
The default hasher is RandomState, the same HashDoS-resistant default used by
std::collections::HashMap. A faster hasher can improve throughput when all keys
are trusted:
use Cache;
let mut cache: = builder
.max_capacity
.build_with_hasher;
How SIEVE Works
Entries remain in insertion order. Each entry has one visited bit, and the cache maintains a hand into the entry list:
- A successful
getmarks the entry as visited without moving it. - When space is needed, the hand scans entries and clears visited bits.
- The first unvisited entry is evicted.
- The new entry is admitted unconditionally.
The hand persists between evictions, so the work of scanning visited entries is
amortized while cache hits remain constant-time table lookups plus one bit write.
peek, contains_key, and iteration do not set the visited bit.
What Was Removed from Mini Moka
| Feature | Rationale |
|---|---|
| Concurrent cache | Eliminates locks and atomics |
| Async support | Avoids a runtime dependency |
| Weight-based eviction | Every entry occupies one slot |
| TTL and TTI expiration | Avoids timer and maintenance overhead |
| W-TinyLFU admission | SIEVE uses a one-bit second-chance policy |
Minimum Supported Rust Version
Micro Moka supports Rust 1.76.0 and uses the Rust 2021 edition. MSRV increases are not considered semver-breaking changes.
Development
# Benchmark dependencies are opt-in.
RUSTFLAGS='--cfg bench_deps'
Releases
Merges to main are released automatically. See RELEASING.md.
Credits
SIEVE was introduced by Yang et al. at NSDI 2024. Micro Moka was forked from Mini Moka by Tatsuya Kawano and the Moka contributors.
License
MIT OR Apache-2.0. See LICENSE-MIT and LICENSE-APACHE.