Spring Boot with Caffeine Cache

Welcome, in this tutorial, we will see how to integrate Caffeine cache in a Spring boot application. Caffeine is a high-performance java8 based caching library and is similar to the in-memory cache provided by google guava api.

1. Introduction

Before going further in this tutorial, we will look at the common terminology such as introduction to Spring Boot and Lombok.

1.1 Spring Boot

  • Spring boot is a module that provides rapid application development feature to the spring framework including auto-configuration, standalone-code, and production-ready code
  • It creates applications that are packaged as jars and are directly started using embedded servlet containers (such as Tomcat, Jetty or, Undertow). Thus, no need to deploy the war files
  • It simplifies the maven configuration by providing the starter template and helps to resolve the dependency conflicts. It automatically identifies the required dependencies and imports them into the application
  • It helps in removing the boilerplate code, extra annotations, and XML configurations
  • It provides powerful batch processing and manages the rest endpoints
  • It provides an efficient JPA-starter library to effectively connect the application with the relational databases
  • It offers a Microservice architecture and cloud configuration that manages all the application related configuration properties in a centralized manner

1.2 Lombok

  • Lombok is nothing but a small library that reduces the amount of boilerplate Java code from the project
  • Automatically generates the getters and setters for the object by using the Lombok annotations
  • Hooks in via the Annotation processor API
  • Raw source code is passed to Lombok for code generation before the Java Compiler continues. Thus, produces properly compiled Java code in conjunction with the Java Compiler
  • Under the target/classes folder you can view the compiled class files
  • Can be used with Maven, Gradle IDE, etc.

1.2.1 Lombok features

valLocal variables are declared as final
varMutable local variables
@Slf4JCreates an SLF4J logger
@CleanupWill call close() on the resource in the finally block
@GetterCreates getter methods for all properties
@SetterCreates setter for all non-final properties
  • Generates implementations of equals(Object other) and hashCode()
  • By default will use all non-static, non-transient properties
  • Can optionally exclude specific properties
  • Generates String of class name, and each field separated by commas
  • Optional parameter to include field names
  • Optional parameter to include a call to the super toString method
  • Generates no-args constructor
  • Will cause compiler error if there are final fields
  • Can optionally force, which will initialize final fields with 0/false/null var – mutable local variables
  • Generates a constructor for all fields that are final or marked @NonNull
  • The constructor will throw a NullPointerException if any @NonNull fields are null val – local variables are declared final
  • Generates a constructor for all properties of the class
  • Any @NotNull properties will have null checks
  • Generates typical boilerplate code for POJOs
  • Combines – @Getter, @Setter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor
  • No constructor is generated if constructors have been explicitly declared
  • Implements the Builder pattern for object creation
  • The immutable variant of @Data
  • All fields are made private and final by default

Let us go ahead with the tutorial implementation but before going any further I’m assuming that you’re aware of the Spring boot basics.

2. Spring Boot with Caffeine Cache

2.1 Tools Used for Spring boot application and Project Structure

We are using Eclipse Kepler SR2, JDK 8, and Maven. In case you’re confused about where you should create the corresponding files or folder, let us review the project structure of the spring boot application.

spring caffeine cache - project structure
Fig. 1: Project structure

Let us start building the application!

3. Creating a Spring Boot application

Below are the steps involved in developing the application.

3.1 Maven Dependency

Here, we specify the dependency for the Spring boot (Web, JPA, Cache, and Spring doc Open API (for swagger interface)), H2 database, Java Faker (to generate the dummy data), Lombok, and Caffeine. The updated file will have the following code.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi=""











3.2 Application properties file

Create a new YML file at the location: SpringbootCaffeinecache/src/main/resources/ and add the following code where we will define –

  • The h2 database connection (such as username, password, and url) and hibernate details
  • The h2 console details will be accessible at the following URL – http://localhost:10095/h2-console in the browser
  • The Swagger UI path will be accessible at the following URL – http://localhost:10095/swagger-ui-custom.html in the browser
  • Caffeine cache configuration. These properties can be configured either through a configuration file or programmatically

You’re free to change the application or the database details as per your wish.

#h2 configuration
#h2 console settings
#browser url = http://localhost:10095/h2-console
#spring jpa settings
#cache configuration
#it can be done either through configuration file or programmatically
#swagger configuration
#browser url = http://localhost:10095/swagger-ui-custom.html

3.3 Java Classes

Let us write the important java class(es) involved in this application. For brevity, we will skip the following classes –

  • – Entity class that will be persisted in the database
  • – Repository interface that extends the JpaRepository interface to perform the SQL operations. The interface also contains a custom method to fetch the product by its reference id
  • – Service class that interact with the DAO layer methods
  • – Bootstrap class to populate dummy data to the h2 database once the application is started successfully
  • – Configuration class to define the Faker bean
  • – Response class will be used for sending a response back to the user

Before we go through the programming stuff we will take a look at a simple flowchart diagram to understand the caching in an application.

spring caffeine cache - cache in app
Fig. 2: Implementing cache in an application

3.3.1 Implementation/Main class

Add the following code to the main class to bootstrap the application from the main method. Always remember, the entry point of the spring boot application is the class containing @SpringBootApplication annotation and the static main method.

package com.springboot.caffeine.cache;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

//lombok annotation
//spring annotation
//to enable the caching support in the application
public class SpringbootCaffeinecacheApplication {

    public static void main(String[] args) {, args);"Springboot caffeine cache application is started successfully");

3.3.2 Controller class

Add the following code to the controller class to specify the different endpoints. The controller methods are annotated with the HTTP GET mapping annotation.

package com.springboot.caffeine.cache.controller;

import com.springboot.caffeine.cache.dto.CommerceDto;
import com.springboot.caffeine.cache.model.Commerce;
import com.springboot.caffeine.cache.service.CommerceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

//spring annotations
//swagger annotation
@Tag(name = "Commerce resource REST endpoints", description = "Shows the product info")
public class CommerceController {

    private CommerceService service;

    //swagger annotations
    @Operation(summary = "Return product list")
    @ApiResponse(responseCode = "200", description = "The request has succeeded")
    //spring annotations
    public List<CommerceDto> getCommerceDtoList() {
        return service.getCommerceList().stream().map(CommerceDto::from).collect(Collectors.toList());

    //NOTE - we are only considering the happy path.
    //you are free to add the failure case where if product not found
    //throw an NotFoundException and return HTTP 404 error response

    //swagger annotations
    @Operation(summary = "Return product by id")
    @ApiResponse(responseCode = "200", description = "The request has succeeded")
    //spring annotations
    public CommerceDto getCommerceById(@RequestParam("id") UUID id) {
        final Commerce c = service.getCommerceByRefId(id);
        return CommerceDto.from(c);

4. Run the Application

To execute the application, right-click on the class, Run As -> Java Application.

spring caffeine cache - run the app
Fig. 3: Run the Application

5. Project Demo

When the application is started, open the Postman tool to hit the application endpoints. You are free to choose any tool of your choice and for this tutorial, we will use the spring swagger interface (accessible at the following URL – http://localhost:10095/swagger-ui-custom.html).

Application endpoints

-- HTTP GET endpoints –

//Endpoint name – Get all products
//URL :: http://localhost:10095/api/commerces

//Endpoint name – Get product by ref id
//URL :: http://localhost:10095/api/commerce?id=e45f2c96-be5b-4779-803c-a611ff5f150f

You can check the logs to understand that once the product is in the cache the subsequent incoming requests are served from the cache. The fresh request will pull the data from the database and save it in the cache so that repeated calls to the database can be avoided. That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

6. Summary

In this section, you learned,

  • Spring boot and Caffeine cache introduction
  • Steps to implement caffeine cache introduction in a spring boot application

You can download the sample application as an Eclipse project in the Downloads section.

7. Download the Project

This was an example to implement caffeine cache in a spring boot application.

You can download the full source code of this example here: Spring Boot with Caffeine Cache


An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button