Configuring Caches

By default Caffeine is used for cache definitions which can be configured via application configuration. For example with application.yml:

Cache Configuration Example

  1. micronaut:
  2. caches:
  3. my-cache:
  4. maximum-size: 20

The above example will configure a cache called “my-cache” with a maximum size of 20.

Naming Caches

Names of caches under micronaut.caches should be defined in kebab case (lowercase and hyphen seperated), if camel case is used the names are normalized to kebab case. So for example specifing myCache will become my-cache. The kebab case form should be used when referencing caches in the @Cacheable annotation.

To configure a weigher to be used with the maximumWeight configuration, create a bean that implements io.micronaut.caffeine.cache.Weigher. To associate a given weigher with only a specific cache, annotate the bean with @Named(<cache name>). Weighers without a named qualifier will apply to all caches that don’t have a named weigher. If no beans are found, a default implementation will be used.

🔗

Table 1. Configuration Properties for DefaultCacheConfiguration
PropertyTypeDescription

micronaut.caches..charset

java.nio.charset.Charset

The charset used to serialize and deserialize values

micronaut.caches..initial-capacity

java.lang.Integer

The initial cache capacity.

micronaut.caches..maximum-size

java.lang.Long

Specifies the maximum number of entries the cache may contain

micronaut.caches..maximum-weight

java.lang.Long

Specifies the maximum weight of entries

micronaut.caches..expire-after-write

java.time.Duration

The cache expiration duration after writing into it.

micronaut.caches..expire-after-access

java.time.Duration

The cache expiration duration after accessing it

micronaut.caches..record-stats

boolean

Set whether record stats is enabled. Default value ({@value #DEFAULT_RECORD_STATS}).

micronaut.caches..test-mode

boolean

Set whether test mode is enabled. Default value ({@value #DEFAULT_TESTMODE}).