Home  >  Spring Core

Example of CachingConfigurer in Spring

By Arvind Rai, August 03, 2018
Spring CachingConfigurer is the interface which comes under the package org.springframework.cache. To use cache in our Spring application we need to define CacheManager bean. For custom cache manager we need to use CachingConfigurer and override cacheManager(), keyGenerator() etc.

1. CacheManager

CacheManager is used to provide custom cache manager. We need to Override it in JavaConfig as following.
@Bean
@Override
public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("test")));
        return cacheManager;
}

2. KeyGenerator

Use KeyGenerator to define cache key. Here we are returning null.
@Bean
@Override
public KeyGenerator keyGenerator() {
	return null;
} 
EnableCachingConfig.java
package com.concretepage;
import java.util.Arrays;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class EnableCachingConfig implements CachingConfigurer {
    @Bean
    public Employee employee() {
        return new Employee();
    }
    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("test")));
        return cacheManager;
    }
    @Bean
    @Override
    public KeyGenerator keyGenerator() {
	return null;
    }
} 
Find other files of our demo application.
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 "Atul";
		} else {
			return "Sudhir";
		}
	}
} 
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..
Atul
Atul
execute method..
Sudhir 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us