How to do it...

To enable Spring Cache in Spring Boot 2.0 application, follow these steps:

  1. Add the starter POM for Spring Boot 2.0 caching:
<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-cache</artifactId> 
</dependency>
  1. For Ehcache auto-configuration, add the following Ehcache provider to the Maven dependencies:
<dependency> 
   <groupId>net.sf.ehcache</groupId> 
   <artifactId>ehcache</artifactId> 
   <version>2.9.0</version> 
</dependency> 
  1. Also add the following Caffeine provider to the Maven dependencies:
<dependency> 
   <groupId>com.github.ben-manes.caffeine</groupId> 
   <artifactId>caffeine</artifactId> 
   <version>2.5.2</version> 
</dependency> 
  1. For Ehcache, copy the ehcache.xml of Chapter 5, Cross-Cutting the MVC, and modify the cache name and diskStore path. Spring Boot's default cache type is Ehcache and it always looks for the ehcache.xml in the src/main/resources/ folder by default:
<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:noNamespaceSchemaLocation="ehcache.xsd" 
   updateCheck="true" 
   monitoring="autodetect" 
   dynamicConfig="true"> 
 
   <diskStore path="C://ch09cached" /> 
 
   <cache name="departmentCache" 
      maxEntriesLocalHeap="10000" 
      maxEntriesLocalDisk="1000" 
      eternal="false" 
      diskSpoolBufferSizeMB="20" 
      timeToIdleSeconds="300" timeToLiveSeconds="600" 
      memoryStoreEvictionPolicy="LFU" 
      transactionalMode="off"> 
      <persistence strategy="localTempSwap" /> 
   </cache> 
</ehcache>
  1. To configure Caffeine cache manager, open application.properties and add the following properties:
spring.cache.cache-names=employeeCache 
spring.cache.caffeine.spec=maximumSize=500, expireAfterAccess=30s 
  1. To finally enable caching, create a context definition class in org.packt.spring.boot.config with the @EnableCaching annotation:
@Configuration 
@EnableCaching 
public class CachingConfig { } 
  1. Open DepartmentRespository and apply Ehcache cache:
@Repository 
public interface EmployeeRepository extends JpaRepository<Employee, Integer> { 
 
   @Cacheable("employeeCache") 
   List<Employee> findByDeptid(Integer deptid); 
    
   @Cacheable("employeeCache") 
   List<Employee> findByFirstname(String firstname); 
    
   @Cacheable("employeeCache") 
   List<Employee> findByLastname(String lastname); 
    
   @Cacheable("employeeCache") 
   List<Employee> findByAge(Integer age); 
    
} 
  1. Open EmployeeRepository and apply Caffeine cache:
@Repository 
public interface EmployeeRepository extends  
         JpaRepository<Employee, Integer> { 
 
   @Cacheable("employeeCache") 
   List<Employee> findByDeptid(Integer deptid); 
    
   @Cacheable("employeeCache") 
   List<Employee> findByFirstname(String firstname); 
    
   @Cacheable("employeeCache") 
   List<Employee> findByLastname(String lastname); 
    
   @Cacheable("employeeCache") 
   List<Employee> findByAge(Integer age); 
} 
  1. Save all files. Then clean and deploy the project ch09-flux.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.180.43