1. Overview

Caffeine cache is a high-performance cache library for Java. In this short tutorial, we’ll see how to use it with Spring Boot.

2. Dependencies

To get started with Caffeine and Spring Boot, we first add the spring-boot-starter-cache and caffeine dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
    </dependency>
</dependencies>

These import the base Spring caching support, along with the Caffeine library.

3. Configuration

Now we need to configure caching in our Spring Boot application.

First, we create a Caffeine bean. This is the main configuration that will control caching behavior such as expiration, cache size limits, and more:

@Bean
public Caffeine caffeineConfig() {
    return Caffeine.newBuilder().expireAfterWrite(60, TimeUnit.MINUTES);
}

Next, we need to create another bean using the Spring CacheManager interface. Caffeine provides its implementation of this interface, which requires the Caffeine object we created above:

@Bean
public CacheManager cacheManager(Caffeine caffeine) {
    CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
    caffeineCacheManager.setCaffeine(caffeine);
    return caffeineCacheManager;
}

Finally, we need to enable caching in Spring Boot using the @EnableCaching annotation. This can be added to any @Configuration class in the application.

4. Examples

With caching enabled and configured to use Caffeine, let’s look at a few examples of how we can use caching in our Spring Boot application.

The primary way to use caching in Spring Boot is with the @Cacheable annotation. This annotation works on any method of a Spring bean (or even the entire class). It instructs the registered cache manager to store the result of the method call in a cache.

A typical usage is inside service classes:

@Service
public class AddressService {
    @Cacheable
    public AddressDTO getAddress(long customerId) {
        // lookup and return result
    }
}

Using the @Cacheable annotation without parameters will force Spring to use default names for both the cache and cache key.

We can override both of these behaviors by adding some parameters to the annotation:

@Service
public class AddressService {
    @Cacheable(value = "address_cache", key = "customerId")
    public AddressDTO getAddress(long customerId) {
        // lookup and return result
    }
}

The example above tells Spring to use a cache named address_cache and the customerId argument for the cache key.

Finally, because the cache manager is itself a Spring bean, we can also autowire it into any other bean and work with it directly:

@Service
public class AddressService {

    @Autowired
    CacheManager cacheManager;

    public AddressDTO getAddress(long customerId) {
        if(cacheManager.containsKey(customerId)) {
            return cacheManager.get(customerId);
        }
        
        // lookup address, cache result, and return it
    }
}

5. Conclusion

In this tutorial, we’ve seen how to configure Spring Boot to use Caffeine cache, along with some examples of how to use caching in our application.

And of course, all of the code examples are located over on GitHub.