InMemoryKache

class InMemoryKache<K : Any, V : Any> : ObjectKache<K, V>

An in-memory coroutine-safe versatile cache that stores objects by keys.

It can be built using the following syntax:

val cache = InMemoryKache<String, String>(maxSize = 100) {
strategy = KacheStrategy.LRU
// Other configuration
}

See also

Types

Link copied to clipboard
class Configuration<K, V>(var maxSize: Long)

Configuration for InMemoryKache. It is used as a receiver of InMemoryKache builder.

Properties

Link copied to clipboard
open override var maxSize: Long

Returns the maximum capacity of this cache, calculated by sizeCalculator.

Link copied to clipboard
open override var size: Long

Returns the current size of the cache, calculated by sizeCalculator.

Functions

Link copied to clipboard
open suspend override fun clear()

Clears the cache, calling EntryRemovedListener on each removed entry with evicted set to false.

Link copied to clipboard
open suspend override fun evictAll()

Removes all keys and their corresponding values from the cache.

Link copied to clipboard
open suspend override fun evictExpired()

Removes all expired keys and their corresponding values from the cache.

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

Returns the value corresponding to the given key if it exists or is currently being created, or null otherwise.

Link copied to clipboard
open suspend override fun getAllKeys(): KacheKeys<K>

Returns a KacheKeys object containing all keys in the cache, including under-creation keys.

Link copied to clipboard
open override fun getIfAvailable(key: K): V?

Returns the value corresponding to the given key, or null if such a key is not present in the cache.

Link copied to clipboard
open override fun getIfAvailableOrDefault(key: K, defaultValue: V): V

Returns the value corresponding to the given key, or defaultValue if such a key is not present in the cache.

Link copied to clipboard
open suspend override fun getKeys(): Set<K>

Returns a read-only Set of keys currently in the cache, excluding under-creation keys.

Link copied to clipboard
open suspend override fun getOrDefault(key: K, defaultValue: V): V

Returns the value corresponding to the given key if it exists or is currently being created, or defaultValue otherwise.

Link copied to clipboard
open suspend override fun getOrPut(key: K, creationFunction: suspend (key: K) -> V?): V?

Returns the value corresponding to the given key if it exists, is currently being created, a new value created by creationFunction, or null if the creation fails.

Link copied to clipboard
open suspend override fun getUnderCreationKeys(): Set<K>

Returns a read-only Set of keys currently under creation.

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

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

open suspend override fun put(key: K, creationFunction: suspend (key: K) -> V?): V?

Associates a new value created by creationFunction with the given key.

Link copied to clipboard
open suspend override fun putAll(from: Map<out K, V>)

Updates this cache with key/value pairs from the specified map from.

Link copied to clipboard
open suspend override fun putAsync(key: K, creationFunction: suspend (key: K) -> V?): Deferred<V?>

Associates a new value created by creationFunction asynchronously with the given key.

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

Removes the specified key and its corresponding value from the cache.

Link copied to clipboard
open suspend override fun removeAllUnderCreation()

Cancels all in-progress creations.

Link copied to clipboard
open suspend override fun resize(maxSize: Long)

Sets the maximum capacity of this cache to maxSize.

Link copied to clipboard
open suspend override fun trimToSize(size: Long)

Remove entries from the cache until the size is less than or equal to size.