LruCache

class LruCache<K, V>(maxSize: Int)

A coroutine-safe Least Recently Used (LRU) cache implementation designed for Kotlin Coroutines.

This cache evicts the least recently accessed items when the maxSize is exceeded. It uses a Mutex to ensure thread safety across suspending functions, making it safe for concurrent access from multiple coroutines.

Operations generally run in O(1) time complexity, subject to lock contention.

Parameters

K

The type of keys maintained by this cache.

V

The type of mapped values.

Constructors

Link copied to clipboard
constructor(maxSize: Int)

Functions

Link copied to clipboard
suspend fun clear()

Removes all of the mappings from this cache. The cache will be empty after this call returns.

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

Returns the value to which the specified key is mapped, or null if this cache contains no mapping for the key.

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

Associates the specified value with the specified key in the cache.

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

Removes the mapping for the specified key from this cache if present.