Sub topic 10
Configuring Caffeine for a local cache
This chapter assumes you can build a Spring Boot application with Maven or Gradle, you understand beans and dependency injection, and you have already enabled caching with @EnableCaching and used @Cacheable. Here you will plug in Caffeine as the cache provider and set its size, expiry, and statistics.
What Caffeine is
Caffeine is an in-memory Java cache library. Entries live inside your application's JVM heap, so reads are fast and need no network call. The trade-off is that each application instance has its own private copy. If you run three instances, you have three separate caches.
Spring Boot detects Caffeine on the classpath and configures a CaffeineCacheManager for you. Your @Cacheable, @CachePut, and @CacheEvict annotations do not change.
Adding the dependencies
Add spring-boot-starter-cache and com.github.ben-manes.caffeine:caffeine. Spring Boot manages the Caffeine version, so you do not need to set one.
Configuring with properties
The simplest setup uses application.properties:
# build.gradle / pom.xml: spring-boot-starter-cache and com.github.ben-manes.caffeine:caffeine
spring.cache.type=caffeine
spring.cache.cache-names=products,productSearch
spring.cache.caffeine.spec=maximumSize=500,expireAfterWrite=10m,recordStatsEach setting does one job:
spring.cache.type=caffeineselects the provider explicitly. Boot would otherwise pick Caffeine automatically when it is the only supported provider found.spring.cache.cache-namescreates the listed caches at startup. Setting it also stops Boot from creating other caches on demand, so a@Cacheablewith an unlisted name fails at runtime.maximumSize=500caps each cache at about 500 entries. When it fills up, Caffeine evicts entries that are least likely to be used again.expireAfterWrite=10mremoves an entry 10 minutes after it was written. Readers never see data older than that.recordStatsmakes Caffeine count hits and misses. Without it, Actuator metrics for the cache show nothing useful.
Expiry options
The spec string accepts two expiry settings:
| Setting | Meaning | Use it when |
|---|---|---|
| `expireAfterWrite=10m` | The entry expires a fixed time after it was created or replaced. | Data goes stale on a known schedule. |
| `expireAfterAccess=10m` | The timer restarts on every read or write. | You want to keep hot entries and drop idle ones. |
Prefer expireAfterWrite for data that can change. A frequently read entry under expireAfterAccess may never expire, so it can stay stale indefinitely.
One spec for all caches
The spring.cache.caffeine.spec value applies to every cache the manager creates. You cannot give products a 10-minute expiry and productSearch a 1-minute expiry with properties alone. If you need per-cache settings, define your own CacheManager bean and register each cache with its own Caffeine builder. Defining that bean replaces the auto-configured one.
Checking the result
Start the application and call a cached method twice. The second call should not reach your slow code. A log line or a counter inside the method makes this easy to see. The Actuator chapter later in this tutorial shows how to read hit and miss counts.
Exercise
A teammate sets spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=10m for a cache of product prices. A popular product is read every minute. What problem can this cause, and which setting fixes it?
Show answer
Each read restarts the 10-minute timer, so the popular product's entry never expires and its price can stay stale indefinitely. Using expireAfterWrite=10m makes the entry expire 10 minutes after it was written, so it is reloaded regularly.