Ktor 3.6.0 Help

Caching

The Ktor client provides the HttpCache plugin for caching previously fetched resources in memory or persistent storage.

Add dependencies

The HttpCache plugin is included in the ktor-client-core artifact and doesn't require any additional dependencies.

In-memory cache

To enable in-memory cache, install HttpCache in the 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) }

By default, the HttpCache plugin stores cached responses in memory.

For example, if you make two consecutive requests to a resource with a configured Cache-Control header, the client can serve the second response from the cache instead of requesting the resource again.

Persistent cache

You can store cached responses persistently by configuring a CacheStorage implementation.

Ktor provides the FileStorage() function, which stores cached responses in the file system. FileStorage() uses kotlinx-io and is available on all supported platforms.

Create a Path for the cache directory and pass it to the FileStorage() function. Then, configure the storage using the publicStorage() or privateStorage() functions:

val client = HttpClient(CIO) { install(HttpCache) { publicStorage(FileStorage(Path("build/cache"))) } }
  • Use the publicStorage() function for responses that can be stored in a shared cache.

  • Use the privateStorage() function for responses intended for a private cache.

25 August 2026