Specify caching options on different levels: globally on the application level, on a route level, or for specific calls.
CachingHeaders, you need to include the
ktor-server-caching-headers artifact in the build script:
... inside the
... inside the explicitly defined
module, which is an extension function of the
CachingHeaders plugin can also be installed to specific routes. This might be useful if you need different
CachingHeaders configurations for different application resources.
CachingHeaders, you can configure caching settings for various content types.
To configure the
CachingHeaders plugin, you need to define the options function to provide specified caching options for a given
ApplicationCall and content type. The code snippet from the caching-headers example shows how to add the
Cache-Control header with the
max-age option for a plain text and HTML:
The CachingOptions object accepts
Expires header values as parameters:
cacheControlparameter accepts a CacheControl value. You can use
CacheControl.MaxAgeto specify the
max-ageparameter and related settings, such as visibility, revalidation options, and so on. You can disable caching by using
expiresparameter allows you to specify the
Expiresheader as a
You can install plugins not only globally but also to specific routes. For instance, the example below shows how to add the specified caching header for the
If you need more fine-grained caching setup, you can configure caching options on a call level using the
ApplicationCall.caching property. The example below shows how to configure caching options depending on whether a user is logged in or not: