In the last example we saw how to use a custom KeyGenerator by using CacheConfig#keyGenerator . In this example, we will see how to register our custom KeyGenerator globally by implementing CachingConfigurer interface or by extending its adapter class CachingConfigurerSupport.
Example
Java Config
CachingConfigurer interface is to be implemented by @Configuration classes annotated with @EnableCaching. It provides various methods to configure or customize caching abstraction.
@Configuration
@ComponentScan
@EnableCaching
public class AppConfig extends CachingConfigurerSupport {
@Override
public KeyGenerator keyGenerator() {
return new MyCustomKeyGenerator();
}
@Bean
@Override
public CacheManager cacheManager() {
List<ConcurrentMapCache> caches = Arrays.asList(new ConcurrentMapCache("employee-cache"));
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(caches);
return cacheManager;
}
}
We are also overriding cacheManager() method to register our cache provider.
The Custom KeyGenerator
package com.logicbig.example;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component("myKeyGen")
public class MyCustomKeyGenerator implements KeyGenerator {
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getSimpleName())
.append("-")
.append(method.getName());
if (params != null) {
for (Object param : params) {
sb.append("-")
.append(param.getClass().getSimpleName())
.append(":").append(param);
}
}
return sb.toString();
}
}
A service bean
As compare to the last example, we don't need to use @CacheConfig#keyGenerator or @Cacheable#keyGenerator anymore.
@Service
@CacheConfig(cacheNames = "employee-cache")
public class EmployeeService {
@Cacheable
public Employee getEmployeeById(int id) {
System.out.println("getEmployeeById() invoked");
return new Employee(id, "Adam", "IT");
}
@Cacheable
public Employee getEmployeeByNameAndDept(String name, String dept) {
System.out.println("getEmployeeByNameAndDept() invoked");
return new Employee(20, name, dept);
}
}
The main class
public class ExampleMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
EmployeeService employeeService = context.getBean(EmployeeService.class);
System.out.println("-- getting employee by id --");
Employee employee = employeeService.getEmployeeById(10);
System.out.println("employee returned: " + employee);
System.out.println("-- getting employee by name and dept --");
employee = employeeService.getEmployeeByNameAndDept("Linda", "Admin");
System.out.println("employee returned: " + employee);
printNativeCache(context);
}
public static void printNativeCache(ApplicationContext context) {
Cache cache = context.getBean(CacheManager.class).getCache("employee-cache");
System.out.println("-- native cache --");
Map<String, Object> map = (Map<String, Object>) cache.getNativeCache();
map.forEach((key, value) -> {
System.out.println(key + " = " + value);
});
}
} -- getting employee by id -- getEmployeeById() invoked employee returned: com.logicbig.example.Employee@6dc17b83 -- getting employee by name and dept -- getEmployeeByNameAndDept() invoked employee returned: com.logicbig.example.Employee@5e0826e7 -- native cache -- EmployeeService-getEmployeeById-Integer:10 = com.logicbig.example.Employee@6dc17b83 EmployeeService-getEmployeeByNameAndDept-String:Linda-String:Admin = com.logicbig.example.Employee@5e0826e7
Example ProjectDependencies and Technologies Used: - spring-context 5.0.4.RELEASE: Spring Context.
- JDK 1.8
- Maven 3.3.9
|