In this post under Spring Caching, I will explain with example what default cache key are used when storing data in the cache and you don’t specify your own cache key.
Spring Caching out of the box determines the cache key using the method parameters.
If a method has no parameters, the key will be “SimpleKey.EMPTY”
If a method has parameters, the key is calculated using the runtime value of method parameters.
Lets see an example, for our example, we will use the below class
Calculator
package caching.package4;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Component;@Componentpublic class Calculator { @Cacheable(cacheNames = "addNumbersWithoutParam_region") public int addNumbersWithoutParam() { System.out.println("Calculating addNumbersWithoutParam"); int result = 1; for(int i = 1; i < 1000000; i++) { result = result + i; } return result; } @Cacheable(cacheNames = "addNumbersWithParam_region") public int addNumbersWithParam(int start, int end) { System.out.println("Calculating addNumbersWithParam"); int result = 1; for(int i = start; i <= end; i++) { result = result + i; } return result; }}
In the above “Calculator” class, I have created two method “addNumbersWithoutParam” with cache region name “addNumbersWithoutParam_region” and “addNumbersWithParam” with cache region name “addNumbersWithParam_region”.
The method “addNumbersWithoutParam” takes no parameters, and method “addNumbersWithParam” takes parameters.
So in case of method “addNumbersWithoutParam” and cache region “addNumbersWithoutParam_region”, the key will be “SimpleKey.EMPTY”
In case of method “addNumbersWithParam” and cache region “addNumbersWithParam_region”, the key will be calculated using the runtime values of the two parameters.
Lets run the above code using the below main class.
Main class
package caching.package4;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.concurrent.ConcurrentMapCacheManager;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@EnableCaching@Configuration@ComponentScan(basePackages = "caching.package4")public class Example4 { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager(); } public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Example4.class); Calculator calculator = annotationConfigApplicationContext.getBean(Calculator.class); System.out.println("-------------------------------------"); System.out.println("First time addNumbersWithoutParam call started"); System.out.println(calculator.addNumbersWithoutParam()); System.out.println("First time addNumbersWithoutParam call finished"); System.out.println("-------------------------------------"); System.out.println("Second time addNumbersWithoutParam call started"); System.out.println(calculator.addNumbersWithoutParam()); System.out.println("Second time addNumbersWithoutParam call finished"); System.out.println("-------------------------------------"); System.out.println("First time addNumbersWithParam(1, 100) call started"); System.out.println(calculator.addNumbersWithParam(1, 100)); System.out.println("First time addNumbersWithParam(1, 100) call finished"); System.out.println("-------------------------------------"); System.out.println("Second time addNumbersWithParam(1, 100) call started"); System.out.println(calculator.addNumbersWithParam(1, 100)); System.out.println("Second time addNumbersWithParam(1, 100) call finished"); System.out.println("-------------------------------------"); System.out.println("First time addNumbersWithParam(1, 500) call started"); System.out.println(calculator.addNumbersWithParam(1, 500)); System.out.println("First time addNumbersWithParam(1, 500) call finished"); System.out.println("-------------------------------------"); System.out.println("Second time addNumbersWithParam(1, 500) call started"); System.out.println(calculator.addNumbersWithParam(1, 500)); System.out.println("Second time addNumbersWithParam(1, 500) call finished"); System.out.println("-------------------------------------"); }}
The output will be
Output
-------------------------------------First time addNumbersWithoutParam call startedCalculating addNumbersWithoutParam1783293665First time addNumbersWithoutParam call finished-------------------------------------Second time addNumbersWithoutParam call started1783293665Second time addNumbersWithoutParam call finished-------------------------------------First time addNumbersWithParam(1, 100) call startedCalculating addNumbersWithParam5051First time addNumbersWithParam(1, 100) call finished-------------------------------------Second time addNumbersWithParam(1, 100) call started5051Second time addNumbersWithParam(1, 100) call finished-------------------------------------First time addNumbersWithParam(1, 500) call startedCalculating addNumbersWithParam125251First time addNumbersWithParam(1, 500) call finished-------------------------------------Second time addNumbersWithParam(1, 500) call started125251Second time addNumbersWithParam(1, 500) call finished-------------------------------------
As you can see from the output when we call “addNumbersWithParam(1, 500)” first time, it doesn’t get the value stored in the cache which was calculated using “addNumbersWithParam(1, 100)”.
The method “addNumbersWithParam(1, 500)” freshly calculates the value.
In this way, Spring Caching generates cache key when we don’t specify our own cache key.