In the previous post under Spring Caching, I have shown how to veto caching but didn’t mention that we can also access the method return value or result in making that decision.
In this post under Spring Caching, I will show how to do that.
For our example, I will use the below class
Calculator
package caching.package11;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Component;@Componentpublic class Calculator { @Cacheable(cacheNames = "addNumbersWithParam_region", key = "#root.args[0] * #root.args[1]", unless = "#result < 50000") 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; }}
I have used the same “@Cacheable” annotation with “unless” attribute.
The only new is “#result” in the SpEL of “unless” attribute.
This “#result” identifier will contain the return value of the method.
In our example, I am telling Spring to not to cache if the result is less than 50000.
Below is the main class for your reference.
Main class
package caching.package11;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.package11")public class Example11 { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager(); } public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Example11.class); Calculator calculator = context.getBean(Calculator.class); 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, 1000) call started"); System.out.println(calculator.addNumbersWithParam(1, 1000)); System.out.println("First time addNumbersWithParam(1, 1000) call finished"); System.out.println("-------------------------------------"); System.out.println("Second time addNumbersWithParam(1, 1000) call started"); System.out.println(calculator.addNumbersWithParam(1, 1000)); System.out.println("Second time addNumbersWithParam(1, 1000) call finished"); System.out.println("-------------------------------------"); }}
Below is the output
Output
First time addNumbersWithParam(1, 100) call startedCalculating addNumbersWithParam5051First time addNumbersWithParam(1, 100) call finished-------------------------------------Second time addNumbersWithParam(1, 100) call startedCalculating addNumbersWithParam5051Second time addNumbersWithParam(1, 100) call finished-------------------------------------First time addNumbersWithParam(1, 1000) call startedCalculating addNumbersWithParam500501First time addNumbersWithParam(1, 1000) call finished-------------------------------------Second time addNumbersWithParam(1, 1000) call started500501Second time addNumbersWithParam(1, 1000) call finished-------------------------------------
As you can see from the output, the result of “addNumbersWithParam(1, 100)” is not cached during the second call as the result is less than 50000.
But the result of “addNumbersWithParam(1, 1000)” is cached during the second call as the result is greater than 50000.
In this way we can use the return value of the method in deciding whether to veto the caching or not.