How to Configure Cache in Spring Boot Applications

Spring-Boot-Tutorials » on Jul 29, 2018 { 3 Comments } By Sivateja

In this article, I will explain step by step how to configure cache in spring boot applications.Β  Caching helps to increase the performance of the application by reducing number of round trips between the database or any expensive resources. In real time we will face the scenarios like we have to execute heavy database query and lets say the data in the database will change very rarely, for this kind of scenarios its not a good idea to hit the database for every call, rather just cache the result at the first time when it hits the database and return the same data again for the other calls.

Steps to configure cache in spring boot applications..

  • In pom.xml add spring cache dependency spring-boot-starter-cache module
  • Enable cache in spring boot application by writing theΒ @EnableCaching annotation to the main class
  • AddΒ @Cacheable annotation to the method which you would like to cache the result


<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">


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

public class SpringBootApp {
	public static void main(String[] args) {, args);



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.RestController;

import com.java4s.model.Customer;

public class SpringJava4sController {

	public List customerInformation() {
		System.out.println("I am from customerInformation");
		List custDetails =  Arrays.asList(
				 * Here you can add your database logic/flow to get the customer details 
				 * For time being I am hard coding 2 values 
				 new Customer(100,"Bank Of America","USA"),
				 new Customer(101,"Bank Of India","India")
		return custDetails;

package com.java4s.model;

public class Customer {
	private int custNo;
	private String name;
	private String country;
	public Customer() {
	public Customer(int custNumber, String name, String country) {
		this.custNo = custNumber; = name; = country;

	public int getCustNo() {
		return custNo;

	public void setCustNo(int custNo) {
		this.custNo = custNo;

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getCountry() {
		return country;

	public void setCountry(String country) { = country;


Run the application at..


I hit/refreshed the above URL several times and still its showing one system out message πŸ™‚ means the cached method executing once and storing its data in the cache with key name cacheCustInfo, and giving the same data back when we make the same service call again.

Note: Cached data will be available until you clear it off manually, spring will not clear the cache automatically after some time. You can integrate some cache providers like EhCache, Redis..or something, so that you can get better control on the cached data.




Clear the Cache

Generally from our side we will clean or flush the cache after any update or delete operations, for that we useΒ @CacheEvict annotation on the required method.

@CacheEvict(value = "cacheCustInfo", allEntries=true) // @CacheEvict will clear the cache when delete any customer info from the database.
    public void removeEmployee(Id customerId) {
        //Database logic will go here to remove the particular customer from the DB.


How to Disable cache

If we would like to disable the cache, no need to remove all the annotations πŸ™‚ just add the below line in the file, it takes care everything for you.


That’s all for now friends πŸ™‚ hope you enjoy the article, you can download and play with the code.


​ ​​

You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


3 Responses to “How to Configure Cache in Spring Boot Applications”
  1. Mohit Sehra says:

    How spring cache works for mvc?
    If i send request first time spring caches the result in cacheCustInfo , but when i send request again does it use the same cache or creates new cacheCustInfo again?
    And what about request from multiple users?

    • Venkatesh says:

      It uses the same Cache until and unless it sense some change. Coming to multiple users question for each user it creates a proxy object

  2. mohiddin says:


    I'm using cache in my springboot code and @cacheable is applied for one of my get methods in restcontroller and even on the method having jdbctemplate.query for fetching the list from postgre database. but the cache is not working and there is no improvement in performance time.i had used loggers inside the method and i found that everytime its going inside the method instead of loading from cache.

    I had followed all the scenarios you had mentioned in the above tutorial.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.