OnHandCache

class OnHandCache(timeSource: TimeSource = TimeSource.Monotonic, cacheLifecycleScope: CoroutineScope = CoroutineScope(SupervisorJob()))

Constructors

Link copied to clipboard
constructor(timeSource: TimeSource = TimeSource.Monotonic, cacheLifecycleScope: CoroutineScope = CoroutineScope(SupervisorJob()))

Functions

Link copied to clipboard
suspend fun clear()
Link copied to clipboard
suspend operator fun <TData> get(key: CacheableInput): MutableStateFlow<TData?>

External read — always returns a flow. If the key has no entry (or has expired), an empty flow (holding null) is created so subscribers can attach and be notified when a future set populates it. Also ensures a mutex exists for this key.

Link copied to clipboard
fun <TData> getOrNull(key: CacheableInput): MutableStateFlow<TData?>?

Internal nullable read — returns null if no entry exists or if the entry has expired, without creating one.

Link copied to clipboard
fun <TData> observe(key: CacheableInput): Flow<TData?>

Non-suspend observation. Returns a cold Flow that, when collected, suspends to ensure the underlying MutableStateFlow exists and forwards its emissions. Useful for property initialization where we can't call the suspend get.

Link copied to clipboard
suspend operator fun set(key: CacheableInput, value: Any?)

Thread-safe write. Ensures a mutex and flow exist, then emits under the per-key lock.

Link copied to clipboard
suspend fun setMaybeWithTtl(key: CacheableInput, value: Any?, ttl: Duration? = null)

Convenience: write with optional TTL. Delegates to set or setWithTtl.

Link copied to clipboard
suspend fun setWithTtl(key: CacheableInput, value: Any?, ttl: Duration)

Thread-safe write with TTL. The entry will be evicted on the next read after ttl has elapsed since this write.

Link copied to clipboard
suspend fun <T : CacheableInput> updateWithRollback(updates: Map<out T, Any?>, action: suspend () -> Unit): Any

Run the action as a transaction, first performing updates. If action doesn't succeed, perform a rollback of all the values of the given keys. The caller MUST use setUnsafe() internally since we already hold the per-key mutexes — calling set() would deadlock.