Spring Caching Simple (@Cacheable) Example Method Level

In this post under Spring Caching, I will show with example how to enable and use caching in Spring application.

For our example, we will use “Calculator” class which has “calculate” method which will perform heavy calculation. It will perform summation from 1 to 1000000.

We cannot perform the calculation every time, so we have to cache the result, so that next time the result is picked from the cache instead of performing the calculation again.

To inform Spring to cache the result of “calculate” method we will annotate the method with “@Cacheable” annotation.

We also need to name the cache where the result is stored and we do it with the help of “name” attribute in “@Cacheable” annotation.

Below is the class structure with Spring Caching annotations

Calculator

package caching.package1;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
public class Calculator {
@Cacheable(cacheNames = "sum")
public int calculate() {
System.out.println("Calculating");
int sum = 0;
for(int i = 1; i < 1000000; i++) {
sum = sum + i;
}
return sum;
}
}

As you can see from the above code, the “calculate” method is annotated with “@Cacheable” annotation and we have given a name to cache as “sum” using the “name” attribute of “@Cacheable” annotation.

Now we have to tell Spring to enable caching functionality in the application.

This we can do by using “@EnableCaching” annotation in the main method.

Below is the main class

Main Class

package caching.package1;
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.package1")
public class Example1 {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Example1.class);
Calculator calculator = annotationConfigApplicationContext.getBean(Calculator.class);
System.out.println("-------------------------------------");
System.out.println("First time call started");
System.out.println(calculator.calculate());
System.out.println("First time call finished");
System.out.println("-------------------------------------");
System.out.println("Second time call started");
System.out.println(calculator.calculate());
System.out.println("Second time call finished");
System.out.println("-------------------------------------");
}
}

As you can see in the above code, the class is annotated with “@EnableCaching” annotation in addition to “@Configuration” and “@ComponentScan” annotation.

Below is the output

Output

-------------------------------------
First time call started
Calculating
1783293664
First time call finished
-------------------------------------
Second time call started
1783293664
Second time call finished
-------------------------------------

As you can see from the output, during the first call, the method is acutally executed. As a result the word “Calculating” is printed to console and total sum is calculated.

During the second call, the method is not executed. Since the word “Calculating” is not printed to console and the result is taken from cache.

In this way we can enable and use caching in Spring applications.

Leave a comment