Ktor 2.1.3 Help

Caching

The Ktor client provides the HttpCache plugin that allows you to save previously fetched resources in an in-memory cache.

Add dependencies

HttpCache only requires the ktor-client-core artifact and doesn't need any specific dependencies.

Install HttpCache

To install HttpCache, pass it to the install function inside a client configuration block:

import io.ktor.client.* import io.ktor.client.engine.cio.* import io.ktor.client.plugins.cache.* //... val client = HttpClient(CIO) { install(HttpCache) }

This is enough to enable the client to save previously fetched resources in a cache. For example, if you make two consequent requests to a resource with the configured Cache-Control header, the client executes only the first request and skips the second one since data is already saved in a cache.

val client = HttpClient(CIO) { install(HttpCache) install(Logging) { level = LogLevel.INFO } } client.get("http://localhost:8080/customer/1") client.get("http://localhost:8080/customer/1") }

You can find a runnable example here: client-caching.

Last modified: 16 September 2022