Cache

class Cache<K, V>

Optimized cache for read-heavy workloads.

Uses optimistic reading with a mutex for writes only. This allows concurrent reads without locking.

Note: Read methods are NOT suspend and return snapshots. Write methods ARE suspend and lock the cache.

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
suspend fun clear()

Clear all entries from the cache.

Link copied to clipboard
fun contains(key: K): Boolean

Check if a key exists in the cache.

Link copied to clipboard
fun get(key: K): V?

Get a value from the cache.

Link copied to clipboard
suspend fun getOrPut(key: K, defaultValue: suspend () -> V): V

Get a value or compute and cache it if missing.

Link copied to clipboard

Check if cache is empty

Link copied to clipboard
fun keys(): Set<K>

Get all keys in the cache.

Link copied to clipboard
suspend fun put(key: K, value: V)

Store a value in the cache.

Link copied to clipboard
suspend fun remove(key: K): V?

Remove a value from the cache.

Link copied to clipboard
fun size(): Int

Get the number of cached entries.

Link copied to clipboard

Get all values in the cache.