Cache

Stores information temporarily to avoid unneeded network requests.

Cache state

Each piece of data in a cache can be in three different states:

  • Up-to-date: the last query was not long ago, we consider its result still valid,

  • Stale: the last query was long enough ago that it deserves to be checked again, but it probably is still valid. In this case, the cache returns the old value and starts a refresh request in the background.

  • Expired: the last query was too long ago for the data to still be valid. In this case, the cache starts a refresh request and will not return a value until the request finishes. All data that was never previously queried is in this state.

Different cache implementations differ on how they transition data from one state to another. Some cache implementations may not have a 'stale' state. As a user of the cache, you may want to force the state of a specific object if you have more knowledge than the cache, in this case you can use update and expire.

Cache chaining

Cache implementations can be chained. A possible scenario for some data that rarely changes can be:

  • Cache the data in memory for 5 minutes,

  • Cache the data in hard storage for 1 hour,

  • Query the data for real afterward.

Cache chaining is instantiated in the opposite order, like iterators (the last in the chain is the first checked, and delegates to the previous one if they do not have the value). The first element of the chain, and therefore the one responsible for actually starting the request, is cache or batchingCache. Note that both have a few implementation differences, it is not recommended to use them directly without chaining under another implementation.

Parameters

Identifier

An identifier representing a cached object. Two identifiers refer to the same object if their equals method returns true.

Failure

The type of possible failures which may happen when requesting a value. If the cached operation cannot fail, or if you're using another error-handling strategy, see InfallibleCache.

Value

The type of cached object.

Functions

Link copied to clipboard
fun <Identifier, Failure, Value : Any> Cache<Identifier, Failure, Value>.cachedInBrowserStorage(storage: Storage, keyPrefix: String, serializeIdentifier: (Identifier) -> String?, serializeValue: (Value) -> String?, deserializeValue: (String) -> Value?): Cache<Identifier, Failure, Value>

In-browser Cache layer.

Link copied to clipboard
fun <Identifier, Failure, Value : Any> Cache<Identifier, Failure, Value>.cachedInLocalStorage(keyPrefix: String, serializeIdentifier: (Identifier) -> String?, serializeValue: (Value) -> String?, deserializeValue: (String) -> Value?): Cache<Identifier, Failure, Value>

In-browser Cache implementation that is shared between tabs and persists when the browser is closed.

Link copied to clipboard
Link copied to clipboard
fun <Identifier, Failure, Value : Any> Cache<Identifier, Failure, Value>.cachedInSessionStorage(keyPrefix: String, serializeIdentifier: (Identifier) -> String?, serializeValue: (Value) -> String?, deserializeValue: (String) -> Value?): Cache<Identifier, Failure, Value>

In-browser Cache implementation that is cleared when the tab is closed.

Link copied to clipboard
open suspend fun expire(id: Identifier)

Tells the cache that the value it stores for id is out of date, and should be queried again the next time it is requested.

abstract suspend fun expire(ids: Collection<Identifier>)

Tells the cache that the value it stores for the given ids are out of date, and should be queried again the next time they are requested.

Link copied to clipboard

Age-based Cache expiration strategy.

Link copied to clipboard
abstract suspend fun expireAll()

Tells the cache that all values are out of date, and should be queried again the next time they are requested.

Link copied to clipboard
abstract operator fun get(id: Identifier): ProgressiveFlow<Failure, Value>

Gets the value associated with an id in this cache.

Link copied to clipboard
open suspend fun update(vararg values: Pair<Identifier, Value>)
abstract suspend fun update(values: Collection<Pair<Identifier, Value>>)

Forces the cache to accept the given values as more recent for their associated identifier than whatever was previously stored.

open suspend fun update(id: Identifier, value: Value)

Forces the cache to accept value as a more recent value for the given id than whatever it was previously storing.