cachedInMemory
In-memory Cache layer.
General behavior
Updates from the previous cache layer are stored in a dictionary. When get is called, results are returned from the dictionary if available. Otherwise, the request is transmitted to the previous layer.
This implementation frees elements only when expire is called. To free memory automatically, add a subsequent layer responsible for it (e.g. expireAfter).
Observability
If multiple callers request the same value concurrently, a single cache request is started. All subscribers receive all events as if they started the request themselves.
The flow returned by Cache.get is infinite: callers can subscribe to it for as long as they want. If a request is started, for any reason, all subscribers to the flow observe the progress events as well as the final result.
When a new request is started, all existing subscribers see the loading events of the new request with the old results. For example, if the previous request gave A
, and the new request has three progress steps followed by the result B
, an existing subscriber will see the values:
A
, doneA
, 25% loadingA
, 50% loadingA
, 75% loadingB
, done
This is useful for GUIs: the application can communicate that a request is ongoing and the value may be outdated, while still having a value to show.
Example
val scope: CoroutineScope = …
val powersOfTwo = cache<Int, Int> { it * 2 }
.cachedInMemory(scope.coroutineContext.job)
.expireAfter(10.minutes, scope)
Parameters
The Job instance in which requests transmitted to the previous layers are started in. Cancelling this job makes the cache unable to query any new values, and cancels any ongoing request.