***
### The Problem
How can we set multiple cache configurations?
Currently, Spring Boot Caffeine autoconfiguration [force all caches to use the same configuration](https://github.com/spring-projects/spring-framework/pull/1506). It means that all Caffeine caches will share the same eviction configuration. This behavior is not desired when we need caches with different life spans. ^ab1966
### The Solution
^315d0f
#### Using [Coffee Boots](https://github.com/stepio/coffee-boots) third-party library.
```xml
<dependency>
<groupId>io.github.stepio.coffee-boots</groupId>
<artifactId>coffee-boots</artifactId>
<version>1.0.1</version>
</dependency>
```
Example of configuration:
```properties
# where `myCache` is a name of the appropriate cache.
coffee-boots.cache.spec.myCache=maximumSize=100000,expireAfterWrite=1m
```
> [!WARNING]
> There is an [open issue to upgrade Coffee Boots to Spring Boot 3](https://github.com/stepio/coffee-boots/issues/164), please check it’s released if you plan to use Spring Boot version 3.
#### Defining beans by yourself
```java
@Bean
public CaffeineCache cacheOne() {
return new CaffeineCache("cache1",
Caffeine.newBuilder()
.expireAfterWrite(30, MINUTES)
.build());
}
@Bean
public CaffeineCache cacheTwo() {
return new CaffeineCache("cache2",
Caffeine.newBuilder()
.expireAfterWrite(60, MINUTES)
.build());
}
@Bean
public CacheManager caffeineCacheManager(List<CaffeineCache> caffeineCaches) {
var cacheManager = new CaffeineCacheManager();
caffeineCaches.forEach(cache -> cacheManager.registerCustomCache(cache.getName(), cache.getNativeCache()));
return cacheManager;
}
```
> [!WARNING]
> In most cases hard-coding the exact caching parameters is not a good idea, so you may get them from properties.
***
**References**:
- [Is it possible to set a different specification per cache using caffeine in spring boot?](https://stackoverflow.com/questions/49885064/is-it-possible-to-set-a-different-specification-per-cache-using-caffeine-in-spri)