Skip to main content

Module managed_cache

Module managed_cache 

Source
Expand description

Rust port of the Java ManagedCache (org.platformlambda.core.util.ManagedCache) — a named, self-expiring (expire-after-write), size-bounded in-memory cache with a process-wide registry. Design record: draft-design-specs/managed-cache-port.md (maintainer-approved 2026-07-27).

Engine: moka (the Caffeine-lineage Rust cache), kept an internal detail behind this wrapper so it can be swapped without touching any consumer. Deliberate, documented divergences from the Java original (design §5):

  • Deterministic eviction (maintainer ruling, 2026-07-27): the store is built with EvictionPolicy::lru() — newcomers are always admitted and the least-recently-used entry is the victim — where Java’s Caffeine uses approximate W-TinyLFU with frequency-based admission plus deliberate HashDoS jitter (no policy switch exists there; a refactoring note is filed with the Java team).
  • The housekeeper is lifecycle-wired (start_housekeeping, called by AppStarter’s essential-services phase) instead of lazily started on first create: create_cache legitimately runs where no Tokio runtime exists (static init, plain tests). Correctness never depends on the sweep in either engine — the store itself enforces expiry on access.
  • Expiry is clamped to a ~100-year ceiling as well as the Java 1 s floor (moka’s builder panics past 1000 years; Java accepts any long).
  • entries() / ManagedCache::get_cache_collection return snapshots where Java hands out live ConcurrentMap views.

Values are type-erased as CacheValue (Arc<dyn Any + Send + Sync>) — the faithful Rust carrier of Java’s Object reference semantics: the Arc clone returned by ManagedCache::get is the analog of Java handing back the same object reference. Convention: one named cache stores one value shape; ManagedCache::get_as returns None on a type mismatch, exactly where Java’s cast would sit.

Java’s SimpleCache is deliberately NOT ported (maintainer ruling): any Java SimpleCache call site ported later maps onto a ManagedCache instance — bounded + self-expiring is a strict superset of SimpleCache’s unbounded lazy expiry. State the parity note once at each adopted site.

Structs§

ManagedCache
A named, self-expiring, size-bounded cache (see the module doc).

Functions§

start_housekeeping
Start the 10-minute housekeeper the lifecycle owns (idempotent). Java starts its sweeper lazily inside the first constructor; here create_cache may run where no Tokio runtime exists, so the lifecycle wires this instead — a documented behavioral no-op (design §5): the sweep only reclaims memory in caches with no subsequent activity; the store itself enforces expiry on access. Must be called within a Tokio runtime (AppStarter::run does — its “essential services” phase).

Type Aliases§

CacheValue
Type-erased cache value — the Rust carrier of Java’s Object.