Cache

Pedestal Cache is a collection of in-process cache algorithms.

Caching is a powerful technique which allows to dramatically reduce I/O requests, making applications faster and more responsive. For more information on our recommendations for cache-aware software architecture, see the Backbone module.

This library provides multiplatform cache algorithms, which can be used in the same manner client- and server-side. Based on coroutines, they are suitable for high-performance applications as well as mobile web pages. Our goal is to keep this library as lightweight as possible.

Cache operations

Caches behave as key-value stores which are able to query the value automatically. The essential operations are:

  • get: to request the value behind a key,

  • expire: to notify the cache that we know the value has changed,

  • update: to notify the cache that we know what the most recent value for a given key is.

Additionally, get returns a long-lived Flow you can subscribe to, to be notified of future changes to the value. This is particularly useful when using reactive UI frameworks (React, Compose…), as it allows to directly communicate to the framework which parts of the UI change, meaning all components reading from the cache automatically update themselves on new values, with no additional code.

Layering

Complex caching strategies are created by composing simpler implementations. Here is an example of a cache which will store values in-memory for 10 minutes:

val scope: CoroutineScope = TODO()

val powersOfTwo = cache<Int, Int> { it * 2 }
.cachedInMemory(scope.coroutineContext.job)
.expireAfter(10.minutes, scope, clock)

println(powersOfTwo[5].now())

The initial layer (here, the cache factory) is an adapter to expose a regular function as a cache instance, to bootstrap layering. It doesn't do any caching itself.

Packages

Link copied to clipboard
common
js
Link copied to clipboard
common

Reactive key-context-value store algorithms represented by the ContextualCache interface.