CacheEvents

class CacheEvents(bufferCapacity: Int = DEFAULT_BUFFER_CAPACITY) : CacheListener

A CacheListener that republishes events as a Flow you can collect.

The bridge from the cache's synchronous, inline callback to idiomatic coroutine consumption:

val events = CacheEvents()
val cache = SemanticCache(embedder, listeners = listOf(events))

scope.launch {
events.events.collect { event -> /* ship it somewhere */}
}

Delivery is best-effort and non-blocking: the underlying MutableSharedFlow has a bounded buffer and drops the oldest buffered event when a subscriber cannot keep up, so a slow (or absent) collector can never stall a lookup. This is the right trade-off for telemetry — losing an event under load is fine, adding latency to the request path is not. Size the buffer for your burst with bufferCapacity, or subscribe from a fast consumer that offloads its own work.

Events published before anyone subscribes are dropped (replay = 0): a subscriber sees events from the moment it starts collecting, not the cache's history. Safe to register on more than one cache.

Parameters

bufferCapacity

how many events to hold for a lagging subscriber before dropping the oldest.

Constructors

Link copied to clipboard
constructor(bufferCapacity: Int = DEFAULT_BUFFER_CAPACITY)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val events: SharedFlow<CacheEvent>

The live stream of events. Cold until collected; a collector sees only events emitted while it runs.

Functions

Link copied to clipboard
open override fun onEvent(event: CacheEvent)

Handles one event. Must be fast, thread-safe, and must not throw (thrown errors are dropped).