CachePolicy

interface CachePolicy

A policy that adds a caching layer to an operation.

Implementations of this interface define how and where data is cached. The execute method will first attempt to retrieve a result from the cache. If the data is not found or has expired, it will execute the provided block, store its result in the cache, and then return it.

Custom implementations: You can provide your own CachePolicy for persistent storage (e.g. multiplatform settings, SQLDelight), or external caches (e.g. Redis). Implement execute to resolve the key (from CacheConfig.key or CacheConfig.keyProvider), look up the cache, and on miss run block and store the result. For invalidation support, implement CacheHandle as well and expose it via your builder or policy.

Key resolution: If the config uses CacheConfig.keyProvider, invoke it at the start of execute to get the key for this request; otherwise use CacheConfig.key.

Functions

Link copied to clipboard
abstract suspend fun <T> execute(block: suspend () -> T): T

Returns a cached result for the configured key if valid and non-expired; otherwise executes block, caches the result, and returns it.