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
// ...
}

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

The max size of this cache in units calculated by SizeCalculator of the cache. This represents the max number of entries if SizeCalculator used the default implementation (returning 1 for each entry),

Link copied to clipboard
open override var size: Long

The current size of this cache in units calculated by SizeCalculator of the cache. This represents the current number of entries if SizeCalculator used the default implementation (returning 1 for each entry),

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()

Evicts all entries, calling EntryRemovedListener on each removed entry with evicted set to true.

Link copied to clipboard
open suspend override fun evictExpired()

Removes all expired entries, calling EntryRemovedListener on each removed entry with evicted set to true.

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

Returns the value for key if it exists in the cache or waits for its creation if it is currently in progress. This returns null if a value is neither cached nor under creation or cannot be created.

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

Returns a KacheKeys instance that represents the keys that are currently in the cache, along with those that are under creation.

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

Returns the value for key if it exists in the cache or null if it doesn't exist or its creation is still in progress.

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

Returns the value for key if it exists in the cache or defaultValue if it doesn't exist or its creation is still in progress.

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

Returns a set of the keys that are currently in the cache, not under-creation keys.

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

Returns the value for key if it exists in the cache or waits for its creation if it is currently in progress. This returns defaultValue if a value is neither cached nor under creation or cannot be created.

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

Returns the value for key if it exists in the cache, its creation is in progress, or it can be created by creationFunction. This returns null if a value is not cached and cannot be created. You can imply that the creation has failed by returning null. Any unhandled exceptions inside creationFunction won't be handled.

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

Returns a set of the keys that are currently under creation.

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

Caches value for key. If there is a previous value or in-progress creation, it will be replaced/cancelled. It returns the previous value if it already exists, or null if it doesn't exist or its creation is still in progress.

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

Creates a new entry for key using creationFunction and returns the new value. Any existing value or in-progress creation of key would be replaced by the new function. This returns null if the value cannot be created. You can imply that the creation has failed by returning null. Any unhandled exceptions inside creationFunction won't be handled.

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

Caches all entries in from. If there is a previous value or in-progress creation for any of the keys, it will be replaced/cancelled.

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

Creates a new entry for key using creationFunction and returns a Deferred. Any existing value or in-progress creation of key would be replaced by the new function. You can imply that the creation has failed by returning null.

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

Removes the entry and in-progress creation for key if it exists. It returns the previous value for key.

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 max size of the cache to maxSize. If the new maxSize is smaller than the previous value, the cache would be trimmed.

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

Remove entries according to the policy defined by strategy until the total of remaining entries is/at/or below size. It won't affect the max size of the cache, allowing it to grow again.