ContainerKache

interface ContainerKache<K : Any, C : Any>

An interface for a container cache. A container cache is a cache that stores a value for each key in a container i.e. a file. It is used to cache files that are too large to be stored in memory. Storing an object in a container cache requires serialization and deserialization logic that is handled by the user.

Properties

Link copied to clipboard
abstract val maxSize: Long

The max size of this cache in bytes. It doesn't include the size of the journal.

Link copied to clipboard
abstract val size: Long

The current size of this cache in bytes. It doesn't include the size of the journal.

Functions

Link copied to clipboard
abstract suspend fun clear()

Clears the cache.

Link copied to clipboard
abstract suspend fun close()

Closes the journal and cancels any in-progress creation.

Link copied to clipboard
abstract suspend fun get(key: String): C?

Returns the container for key if it exists in the cache or waits for its creation if it is currently in progress. This returns null if a file is not cached and isn't in creation or cannot be created. It may even throw exceptions for unhandled exceptions in the currently in-progress creation block.

Link copied to clipboard
abstract suspend 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
abstract suspend fun getIfAvailable(key: String): C?

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

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

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

Link copied to clipboard
abstract suspend fun getOrPut(key: String, creationFunction: suspend (C) -> Boolean): C?

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

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

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

Link copied to clipboard
abstract suspend fun put(key: String, creationFunction: suspend (C) -> Boolean): C?

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

Link copied to clipboard
abstract suspend fun putAsync(key: String, creationFunction: suspend (C) -> Boolean): Deferred<C?>

Creates a new container for key using creationFunction and returns a Deferred. Any existing container 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
abstract suspend fun remove(key: String)

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

Link copied to clipboard
abstract suspend fun removeAllUnderCreation()

Cancels all in-progress creations.

Link copied to clipboard
abstract suspend 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
abstract suspend 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.