2020-02-07

Spring Caffeine Cache

Caffeine

Caffeine is a Java 8 rewrite of Guava’s cache that supersedes support for Guava. If Caffeine is
present, a CaffeineCacheManager (provided by the spring-boot-starter-cache “Starter”) is autoconfigured.
Caches can be created on startup by setting the configprop:spring.cache.cache-names[]
property and can be customized by one of the following (in the indicated order):
1. A cache spec defined by spring.cache.caffeine.spec
2. A com.github.benmanes.caffeine.cache.CaffeineSpec bean is defined
3. A com.github.benmanes.caffeine.cache.Caffeine bean is defined
For instance, the following configuration creates cache1 and cache2 caches with a maximum size of
500 and a time to live of 10 minutes

spring.cache.cache-names=cache1,cache2
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s

If a com.github.benmanes.caffeine.cache.CacheLoader bean is defined, it is automatically associated
to the CaffeineCacheManager. Since the CacheLoader is going to be associated with all caches managed
by the cache manager, it must be defined as CacheLoader<Object, Object>. The auto-configuration
ignores any other generic type.

No comments:

Post a Comment