一、添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:///POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.edu.tju</groupId>
    <artifactId>springbootcache</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.6</version>
        </dependency>



    </dependencies>



</project>

二、在resources下创建ehcache配置文件ehcache.xml,
其中配置了3个cache,默认cache、user、admin(按需配置)

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <diskStore path="java.io.tmpdir" />


    <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600"
                  timeToLiveSeconds="600" overflowToDisk="true" />

    <cache name="user"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           maxElementsOnDisk="10000000"
           overflowToDisk="true"
           memoryStoreEvictionPolicy="LRU" />

    <cache name="admin"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           maxElementsOnDisk="10000000"
           overflowToDisk="true"
           memoryStoreEvictionPolicy="LRU" />

</ehcache>

三、在application.properties中指定ehcache配置文件

spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:/ehcache.xml

四、启动类增加@EnableCaching注解

package cn.edu.tju;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class, args);
    }
}

五、在controller中使用ehcahe

package cn.edu.tju.controller;

import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @RequestMapping("/getUser/{id}")
    @Cacheable(value="user",key = "#id")
    public String getUser(@PathVariable Integer id){
        System.out.println("get user:"+id);
        return "user: "+id;
    }

    @RequestMapping("/updateUser/{id}")
    @CachePut(value = "user", key = "#id")
    public String updateUser(@PathVariable Integer id) {
        return "user[update]: "+id;
    }

    @RequestMapping("/deleteUser/{id}")
    @CachePut(value = "user", key = "#id")
    public String deleteUser(@PathVariable Integer id) {
        return "user[delete]: "+id;
    }

}