Spring Boot – How to enable and disable cache

Cache is one of the spring boot’s nice features. Caching is a temporary storage mechanism between continuous storage and application. It reduces the number of hits in the database. Caching improves application performance by reducing network connectivity and loading the database.



Steps to Enable cache in Spring Boot

There are three steps to enable Spring Boot cache.

  • In the pom.xml file, add spring boot cache dependency “spring-boot-starter-cache” module.
  • The next step is to enable cache by adding annotation @EnableCaching to the main method.
  • Add @Cacheable annotation to the cached method.

The detailed description of the spring boot cache is available in this article How to configure cache



Steps to Disable cache in Spring Boot

Caching improves the performance of the application. Cache will force the application to restart when the developer changes the code or debug issue. Most of the time, it is necessary to disable cache temporarily to fix problems quickly. Normally, we need to comment the code manually in all references. Spring boot support to disable the cache globally by configuring it in one place.

Here, we’ll see different ways to disable spring boot cache.

1. Application.properties file

spring.cache.type=none

2. Application.yml file

spring:
  cache:
    type: "none"

3. SpringBootApplication main method

package com.yawintutor.cache;

import java.util.Properties;

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

@SpringBootApplication
@EnableCaching
public class SpringBootEnableDisableCacheApplication {

	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(SpringBootEnableDisableCacheApplication.class);
		Properties properties = new Properties();
		properties.setProperty("spring.cache.type", "none");
		application.setDefaultProperties(properties);
		application.run(args);
	}

}

4. As command line argument

java -Dspring.cache.type=none -jar SpringBootEnableDisableCache-0.0.1.jar



Related Articles

2 Comments

  • zhuguowei , May 23, 2020 @ 7:14 AM

    I have tried this and this way does not work

    • Yawin Tutor , May 23, 2020 @ 11:41 PM

      Hi zhuguowei,
      After we create and test, we add the programs. We ‘re really sorry about this. We ‘re going to revisit this post. In the meantime, if you share your code, we can help with that.

      Thanks for visiting our website.

      -Yawin Tutor

Leave a Reply

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