AdSense

網頁

2020/7/23

Spring Boot Caching @CacheEvict作用

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的方法被調用時觸發清除快取資料的結果。


沒有留言:

AdSense