Spring Boot Caching Examples

Spring Boot Caching with Examples

In this article, I am going to discuss Spring Boot Caching with Examples. Please read our previous article where we discussed Spring Boot Caching Introduction.

Spring Boot Caching Examples

In the previous chapter, we learned about caching. Now we will implement Caching in Spring Boot Application. Follow the steps below:

Step 1: Create a new project using Spring Initializr in VS Code. Include the following dependencies:

  • Spring Web
  • Spring Cache Abstraction

Spring Boot Caching with Examples

Ensure that the dependencies are installed:

Spring Boot Caching with Examples

Step 2: Modify the CachingApplication.java file located in the src/main/java/com/dotnet/caching director:

Spring Boot Caching with Examples

We have performed the following modifications:

  • Imported the required package.
  • Add @EnableCaching annotation to enable annotation.

Step 3: Create a directory with its path as src/main/java/com/dotnet/model directory. This directory will be used to store POJO classes.

Step 4: Create a class called Customer.java in the newly created src/main/java/com/dotnet/ model directory. This class is a POJO class.

Step 5: Define the following variables in the class:

Spring Boot Caching with Examples

Step 6: Add getters and setters to the class (use the “Generate Getters and Setters” option in VS Code):

Spring Boot Caching with Examples

Step 7: Add two constructors to the class (use the “Generate Constructors” option in VS Code):

Spring Boot Caching with Examples

Step 8: Create a directory with its path as src/main/java/com/dotnet/controller directory. This directory will be used to store the controller class.

Step 9: Create a class called CustomerController.java in the newly created src/main/java/com/dotnet/controller directory.

Step 10: Modify the newly created CustomerController.java as follows:

Spring Boot Caching with Examples

We have done the following changes:

  • Imported required packages.
  • Added @RestController annotation to the class. This marks the class as a REST controller.
  • Added a function that returns some data as a List. This shall execute whenever a client sends a GET request to http://localhost:8080/customerinfo.

Step 11: Compile and execute the application. Ensure compilation is successful:

Spring Boot Caching with Examples

Congratulations! You now know how to implement caching in Spring Boot.

The Complete Example Code
src/main/java/com/dotnet/caching/CachingApplication.java
package com.dotnet.caching;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class CachingApplication {

    public static void main(String[] args) {
        SpringApplication.run(CachingApplication.class, args);
    }
}
src/main/java/com/dotnet/controller/CustomerController.java
package com.dotnet.controller;

import com.dotnet.model.Customer;

import java.util.Arrays;
import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController
{
    @RequestMapping(value = "/customerinfo", method = RequestMethod.GET)
    @Cacheable(value = "customerinfo")
    public List<Customer> customerInfo()
    {
        System.out.println("Customer information from cache: ");

        //adding customer details in the list
        return Arrays.asList(new Customer(12345,"Customer 1","Savings",100000.00),
                                    new Customer(67890,"Customer 2","Savings",5000.00));
    }
}
src/main/java/com/dotnet/model/Customer.java
package com.dotnet.model;

public class Customer
{
    private int acNo;
    private String name;
    private String acType;
    private double bal;

    public int getAcNo()                    {return acNo;}
    public void setAcNo(int acNo)           {this.acNo = acNo;}
    public String getName()                 {return name;}
    public void setName(String name)        {this.name = name;}
    public String getAcType()               {return acType;}
    public void setAcType(String acType)    {this.acType = acType;}
    public double getBal()                  {return bal;}
    public void setBal(double bal)          {this.bal = bal;}
    
    public Customer(int acNo, String name, String acType, double bal) {
        this.acNo = acNo;
        this.name = name;
        this.acType = acType;
        this.bal = bal;
    }
    public Customer() {
    }    
}

In the next article, I am going to discuss Spring Boot Cache Providers. Here, in this article, I try to explain Spring Boot Caching Examples. I hope you enjoy this Spring Boot Caching Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *