Spring Caching的@CacheEvict
的作用如下。
@CacheEvict
的用途為清除快取。@CacheEvict
標註的方法被調用時會清除指定快取中的資料。
先參考「Spring Boot Caching simple ConcurrentHashMap 範例」設定一簡單快取。
在DemoController
新增一API/employee/clean-cache
用來呼叫清除快取的方法。
DemoController
package com.abc.demo.controller;
import com.abc.demo.model.Employee;
import com.abc.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/employee/{id}")
public Employee getEmployee(@PathVariable Long id) {
return employeeService.getEmployeeById(id);
}
@GetMapping("/employee/clean-cache")
public void cleanEmployeeCache() {
employeeService.cleanEmployeeCache(); // 清除快取
}
}
在EmployeeService
新增一方法cleanEmployeeCache()
並加上@CacheEvict
,則此方法被呼叫時會清除快取。屬性allEntries
設為true
時快取中的資料會全部被清除,否則只會清除指定key(也就是輸入參數組合)的快取資料。
EmployeeService
package com.abc.demo.service;
import com.abc.demo.model.Employee;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class EmployeeService {
@Cacheable(cacheNames = "employee_cache")
public Employee getEmployeeById(Long id) {
Map<Long, Employee> currentEmployeeMap = getCurrentEmployeeMap();
return currentEmployeeMap.get(id);
}
private Map<Long, Employee> getCurrentEmployeeMap() {
Map<Long, Employee> currentEmployeeMap = new HashMap<>();
currentEmployeeMap.put(1L, Employee.builder().id(1L).name("alan").age(33).build());
currentEmployeeMap.put(2L, Employee.builder().id(2L).name("bill").age(45).build());
currentEmployeeMap.put(3L, Employee.builder().id(3L).name("carl").age(24).build());
currentEmployeeMap.put(4L, Employee.builder().id(4L).name("dave").age(31).build());
return currentEmployeeMap;
}
@CacheEvict(cacheNames = "employee_cache", allEntries = true) // 清除employee_cache快取中的所有快取
public void cleanEmployeeCache() {
System.out.println("clean all data in employee_cache");
}
}
用Postman呼叫http://localhost:8080/demo/employee/1
產生快取資料,因此重複呼叫上面的url會從快取中取得結果。一旦呼叫http://localhost:8080/demo/employee/clean-cache
後,則可發現下一次再呼叫http://localhost:8080/demo/employee/1
時會再次進入EmployeeService.getEmployeeById()
方法內去查詢資料,因為標有@CacheEvict
的方法被調用時觸發清除快取資料的結果。
沒有留言:
張貼留言