InMemoryStore

class InMemoryStore(maxEntries: Int = DEFAULT_MAX_ENTRIES, ttl: Duration? = null, clock: Clock = Clock.systemUTC(), maxBytes: Long? = null, listener: CacheListener? = null) : CacheStore

Default CacheStore: entries in a map, search by scanning them.

Good enough to start with and honest about its ceiling. Search is a linear scan of every entry in the scope, so cost grows with cache size — at the default 10,000 entries and 1,536-dimensional vectors a lookup is well under a millisecond, which is nothing next to the network call it replaces. Past roughly 100,000 entries, or as soon as you want the cache shared between instances or to survive a restart, move to a real vector store.

Eviction is least-recently-used, not least-recently-written, and "used" means served: only a confirmed hit calls touch. An entry that keeps scoring second place never gets credit for it.

Every method takes a mutex, so the store is safe to share across coroutines and threads.

Parameters

maxEntries

hard cap across all scopes; the least recently used entry goes first.

ttl

how long an entry stays valid, or null to keep entries until they are evicted. Expired entries are never returned by search and are dropped as they are encountered.

clock

time source; substitute a fixed clock in tests instead of sleeping.

maxBytes

optional cap on estimated resident bytes (embeddings dominate: dimensions * 4 each), evicted least-recently-used just like maxEntries, so a cache in a memory-constrained service cannot grow without bound. null (the default) bounds by count only.

listener

optional sink notified with a CacheEvent.Eviction whenever an entry is evicted (for capacity or memory) or dropped for being expired — the store owns eviction, so the store is what reports it. Called inline while the store's lock is held, so it must be fast and non-blocking (see CacheListener); pass the same listener you give dev.kmemo.SemanticCache to see the whole event stream in one place. null (the default) emits nothing.

Constructors

Link copied to clipboard
constructor(maxEntries: Int = DEFAULT_MAX_ENTRIES, ttl: Duration? = null, clock: Clock = Clock.systemUTC(), maxBytes: Long? = null, listener: CacheListener? = null)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open suspend override fun clear(scope: String? = null)

Removes every entry in scope, or the whole store when scope is null.

Link copied to clipboard
suspend fun purgeExpired(): Int

Drops every expired entry and returns how many were removed.

Link copied to clipboard
open suspend override fun put(entry: CacheEntry)

Writes entry, replacing any existing entry with the same CacheEntry.id.

Link copied to clipboard
open suspend override fun remove(id: String): Boolean

Removes the entry id. Returns true if an entry was actually removed.

Link copied to clipboard
open suspend override fun search(scope: String, embedding: FloatArray, limit: Int): List<ScoredEntry>

Returns up to limit entries from scope closest to embedding, best first.

Link copied to clipboard
open suspend override fun size(scope: String? = null): Int

Number of live entries in scope, or in the whole store when scope is null.

Link copied to clipboard
suspend fun stats(): InMemoryStoreStats

Current size and lifetime eviction counters.

Link copied to clipboard
open suspend override fun touch(id: String)

Signals that the entry id was actually served to a caller.