Home  >  Spring Core

Example of @EnableCaching in Spring

By Arvind Rai, July 30, 2018
On this page we will provide Spring @EnableCaching example. The annotation @EnableCaching enables caching in our Spring application using JavaConfig. To enable caching we need to annotate our JavaConfig with @EnableCaching annotation. To cache a result of a method of a Spring bean we need to annotate that method using @Cacheable annotation. We need to create CacheManager bean in JavaConfig to configure cache. We are using SimpleCacheManager in our example for the demo. Find the code snippet of our JavaConfig.
@Configuration
@EnableCaching
public class EnableCachingConfig {
	@Bean
        public CacheManager cacheManager() {
           SimpleCacheManager cacheManager = new SimpleCacheManager();
           cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("test")));
           return cacheManager;
        }
        ------		
} 

Complete Code

build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'SpringCache'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.springframework.boot:spring-boot-starter:1.2.7.RELEASE'
}  
Employee.java
package com.concretepage;
import org.springframework.cache.annotation.Cacheable;
public class Employee {
        @Cacheable(value = { "test" })
	public String getEmployee(int empId) {
		System.out.println("execute method..");
		if (empId == 1) {
			return "Ram";
		} else {
			return "Sudhir";
		}
	}
} 
EnableCachingConfig.java
package com.concretepage;
import java.util.Arrays;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class EnableCachingConfig {
	@Bean
        public Employee employee() {
           return new Employee();
        }
	@Bean
        public CacheManager cacheManager() {
           SimpleCacheManager cacheManager = new SimpleCacheManager();
           cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("test")));
           return cacheManager;
        }
}
EnableCachingTest.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class EnableCachingTest {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
 
		ctx.register(EnableCachingConfig.class);
		ctx.refresh();

		Employee employee = ctx.getBean(Employee.class);
		
		// calling getEmployee method first time.
		System.out.println(employee.getEmployee(1));

		// calling getEmployee method second time. This time, method will not
		// execute.
		System.out.println(employee.getEmployee(1));

		// calling getEmployee method third time with different value.
		System.out.println(employee.getEmployee(2));
	}
}
Output
execute method..
Ram
Ram
execute method..
Sudhir 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us