1. 程式人生 > 其它 >springboot-cache,快取使用

springboot-cache,快取使用

技術標籤:javajava快取spring

適用於同一入參的快取:

 <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.0</version>
        </dependency>

        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>

        <!--swagger2文件-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.yzgu.</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>


        <!--springboot單元測試-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

編寫 快取測試類


/**
 *  springboot-cache  yzgu
 */

@RequestMapping("/cache")
@Controller
public class HomeCacheController {

    @Autowired
    private TransportRecordService transportRecordService;

    @RequestMapping("/hello")
    @ResponseBody
    @Cacheable(value = "helloCache")
    public String home(){
        System.out.println("測試是否走快取");
        return "hello world!";
    }

    /**
     * 這裡根據入參可以快取資料,只要是name 多次訪問,就直接從快取中獲取,不會一直查詢資料庫
     * @param name
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object test(String name){
        List<TransportRecord> list = transportRecordService.getList(name);
        System.out.println("測試list是否走快取");
        return list;
    }


        /**
         *  這裡 指定的list  是 上面方法 設定的快取
         *  cacheNames  list 上面快取的大key
         *   key:指定要清除快取中的某條資料 key ="123" name 引數 為123的快取
          * allEntries=true:刪除快取中的所有資料
         *
         *   ,beforeInvocation = true 使用在方法之前執行的好處: 如果方法出現異常,快取依舊會被刪除
         *    , allEntries = true
         * @param
         * @param
         * @param
         * @return
         */
    @RequestMapping("/evict")
    @ResponseBody
    @CacheEvict(cacheNames = "list",key = "#name" ) // 清除快取
    public Object evict(String name){
        return "ok";
    }

    /**
     *   當 name 的長度小於4  才快取
     * @param name
     * @return
     */
    @RequestMapping("/condition")
    @ResponseBody
    @org.springframework.cache.annotation.Cacheable(value = "conditionCache", condition = "#name.length() <= 4")
    public Object test2(String name){
        System.out.println("測試condition是否走快取");
        return "abc" + name;
    }

上面的方法

1: 呼叫http://localhost:9002/yzgu/cache/list?name=123

後端會快取 name =123的那條記錄

2: 再次查詢,不會走service 層

呼叫結果如下

3、呼叫 name = 456 也會快取

http://localhost:9002/yzgu/cache/list?name=456

4、 然後清除單條快取 123的快取

http://localhost:9002/yzgu/cache/evict?name=123

再次呼叫http://localhost:9002/yzgu/cache/list?name=123

還是會走資料庫查詢

如果 想清楚所有快取 可以指定