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



Leave a Reply