Spring Framework supports declarative annotation-based caching. The caching abstraction provided by Spring, allows integrating different caching providers without impacting the application code.
Example
Spring provides various annotations. In following example we are going to see a very basic use of them.
A bean using caching annotations
package com.logicbig.example;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
@CacheConfig(cacheNames = "employee-cache")
public class EmployeeService {
@Cacheable
public Employee getEmployeeById(int id) {
System.out.println("getEmployeeById() invoked");
//just returning test employee
return new Employee(id, "Adam", "IT");
}
@Cacheable
public Employee getEmployeeByName(String name) {
System.out.println("getEmployeeByName() invoked");
//just returning test employee
return new Employee(20, name, "Admin");
}
}
@CacheConfig annotation provides a mechanism for sharing common cache-related settings at the class level.
@Cacheable annotation declares that the return value of invoking the target method (or all methods in a class if used on the class level) can be cached. Each time the method is invoked, it is checked whether the method has been already invoked for the given arguments. If no value is found in the cache, the target method will be invoked and the returned value will be stored in the associated cache.
Java Config
package com.logicbig.example;
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.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.List;
@Configuration
@ComponentScan
@EnableCaching
public class AppConfig {
@Bean
public CacheManager cacheManager() {
List<ConcurrentMapCache> caches = Arrays.asList(new ConcurrentMapCache("employee-cache"));
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(caches);
return cacheManager;
}
}
@EnableCaching enables Spring's annotation-driven cache management capability
For Spring caching to work, we also have to register an implementation of CacheManager as seen above.
The interface
CacheManager is Spring's central cache manager SPI. It allows for retrieving named Cache regions.
In this example, we are using Spring provided SimpleCacheManager which uses a given collection of Cache . This is useful for testing or simple caching declarations.
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);
System.out.println("-- getting employee by same id again --");
employee = employeeService.getEmployeeById(10);
System.out.println(employee);
System.out.println("-- getting employee by different id --");
employee = employeeService.getEmployeeById(11);
System.out.println(employee);
System.out.println("-- getting employee by name --");
employee = employeeService.getEmployeeByName("Linda");
System.out.println(employee);
System.out.println("-- getting employee by same name again --");
employee = employeeService.getEmployeeByName("Linda");
System.out.println(employee);
}
} -- getting employee by id -- getEmployeeById() invoked com.logicbig.example.Employee@59fd97a8 -- getting employee by same id again -- com.logicbig.example.Employee@59fd97a8 -- getting employee by different id -- getEmployeeById() invoked com.logicbig.example.Employee@61862a7f -- getting employee by name -- getEmployeeByName() invoked com.logicbig.example.Employee@441772e -- getting employee by same name again -- com.logicbig.example.Employee@441772e
Example ProjectDependencies and Technologies Used: - spring-context 5.0.4.RELEASE: Spring Context.
- JDK 1.8
- Maven 3.3.9
|