­

Spring Boot 2.X(七):Spring Cache 使用

  • 2019 年 10 月 15 日
  • 筆記

Spring Cache 簡介

在 Spring 3.1 中引入了多 Cache 的支援,在 spring-context 包中定義了org.springframework.cache.Cacheorg.springframework.cache.CacheManager 兩個介面來統一不同的快取技術。Cache 介面包含快取的常用操作:增加、刪除、讀取等。CacheManager 是 Spring 各種快取的抽象介面。
Spring 支援的常用 CacheManager 如下:

CacheManager 描述
SimpleCacheManager 使用簡單的 Collection 來存儲快取
ConcurrentMapCacheManager 使用 java.util.ConcurrentHashMap 來實現快取
NoOpCacheManager 僅測試用,不會實際存儲快取
EhCacheCacheManger 使用EhCache作為快取技術。EhCache 是一個純 Java 的進程內快取框架,特點快速、精幹,是 Hibernate 中默認的 CacheProvider,也是 Java 領域應用最為廣泛的快取
JCacheCacheManager 支援JCache(JSR-107)標準的實現作為快取技術
CaffeineCacheManager 使用 Caffeine 作為快取技術。用於取代 Guava 快取技術。
RedisCacheManager 使用Redis作為快取技術
HazelcastCacheManager 使用Hazelcast作為快取技術
CompositeCacheManager 用於組合 CacheManager,可以從多個 CacheManager 中輪詢得到相應的快取

Spring Cache 提供了 @Cacheable 、@CachePut 、@CacheEvict 、@Caching 等註解,在方法上使用。通過註解 Cache 可以實現類似事務一樣、快取邏輯透明的應用到我們的業務程式碼上,且只需要更少的程式碼。
核心思想:當我們調用一個方法時會把該方法的參數和返回結果最為一個鍵值對存放在快取中,等下次利用同樣的參數來調用該方法時將不會再執行,而是直接從快取中獲取結果進行返回。

Cache註解

1.@EnableCaching

開啟快取功能,一般放在啟動類上。

2.@CacheConfig

當我們需要快取的地方越來越多,你可以使用@CacheConfig(cacheNames = {"cacheName"})註解在 class 之上來統一指定value的值,這時可省略value,如果你在你的方法依舊寫上了value,那麼依然以方法的value值為準。

3.@Cacheable

根據方法對其返回結果進行快取,下次請求時,如果快取存在,則直接讀取快取數據返回;如果快取不存在,則執行方法,並把返回的結果存入快取中。一般用在查詢方法上
查看源碼,屬性值如下:

屬性/方法名 解釋
value 快取名,必填,它指定了你的快取存放在哪塊命名空間
cacheNames 與 value 差不多,二選一即可
key 可選屬性,可以使用 SpEL 標籤自定義快取的key
keyGenerator key的生成器。key/keyGenerator二選一使用
cacheManager 指定快取管理器
cacheResolver 指定獲取解析器
condition 條件符合則快取
unless 條件符合則不快取
sync 是否使用非同步模式,默認為false

4.@CachePut

使用該註解標誌的方法,每次都會執行,並將結果存入指定的快取中。其他方法可以直接從響應的快取中讀取快取數據,而不需要再去查詢資料庫。一般用在新增方法上
查看源碼,屬性值如下:

屬性/方法名 解釋
value 快取名,必填,它指定了你的快取存放在哪塊命名空間
cacheNames 與 value 差不多,二選一即可
key 可選屬性,可以使用 SpEL 標籤自定義快取的key
keyGenerator key的生成器。key/keyGenerator二選一使用
cacheManager 指定快取管理器
cacheResolver 指定獲取解析器
condition 條件符合則快取
unless 條件符合則不快取

5.@CacheEvict

使用該註解標誌的方法,會清空指定的快取。一般用在更新或者刪除方法上
查看源碼,屬性值如下:

屬性/方法名 解釋
value 快取名,必填,它指定了你的快取存放在哪塊命名空間
cacheNames 與 value 差不多,二選一即可
key 可選屬性,可以使用 SpEL 標籤自定義快取的key
keyGenerator key的生成器。key/keyGenerator二選一使用
cacheManager 指定快取管理器
cacheResolver 指定獲取解析器
condition 條件符合則快取
allEntries 是否清空所有快取,默認為 false。如果指定為 true,則方法調用後將立即清空所有的快取
beforeInvocation 是否在方法執行前就清空,默認為 false。如果指定為 true,則在方法執行前就會清空快取

6.@Caching

該註解可以實現同一個方法上同時使用多種註解。可從其源碼看出:

public @interface Caching {        Cacheable[] cacheable() default {};        CachePut[] put() default {};        CacheEvict[] evict() default {};    }

Spring Cache 使用

1.構建項目,添加依賴

<?xml version="1.0" encoding="UTF-8"?>  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">      <modelVersion>4.0.0</modelVersion>      <parent>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-parent</artifactId>          <version>2.1.9.RELEASE</version>          <relativePath/> <!-- lookup parent from repository -->      </parent>      <groupId>cn.zwqh</groupId>      <artifactId>spring-boot-cache</artifactId>      <version>0.0.1-SNAPSHOT</version>      <name>spring-boot-cache</name>      <description>spring-boot-cache</description>        <properties>          <java.version>1.8</java.version>      </properties>        <dependencies>            <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-web</artifactId>          </dependency>            <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-test</artifactId>              <scope>test</scope>          </dependency>          <!-- Spring Cache -->          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-cache</artifactId>          </dependency>          <!-- jdbc -->          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-starter-jdbc</artifactId>          </dependency>            <!-- 熱部署模組 -->          <dependency>              <groupId>org.springframework.boot</groupId>              <artifactId>spring-boot-devtools</artifactId>              <optional>true</optional> <!-- 這個需要為 true 熱部署才有效 -->          </dependency>              <!-- mysql 資料庫驅動. -->          <dependency>              <groupId>mysql</groupId>              <artifactId>mysql-connector-java</artifactId>              <scope>runtime</scope>          </dependency>            <!-- mybaits -->          <dependency>              <groupId>org.mybatis.spring.boot</groupId>              <artifactId>mybatis-spring-boot-starter</artifactId>              <version>2.1.0</version>          </dependency>        </dependencies>        <build>          <plugins>              <plugin>                  <groupId>org.springframework.boot</groupId>                  <artifactId>spring-boot-maven-plugin</artifactId>              </plugin>          </plugins>      </build>    </project>  

2.application.properties 配置文件

#datasource  spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver  spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_test?useUnicode=true&characterEncoding=UTF-8&useSSL=true  spring.datasource.username=root  spring.datasource.password=123456  #mybatis  mybatis.mapper-locations=classpath:/mapper/*Mapper.xml

3.實體類

public class UserEntity implements Serializable{        /**       *       */      private static final long serialVersionUID = 5237730257103305078L;      private Long id;      private String userName;      private String userSex;      public Long getId() {          return id;      }      public void setId(Long id) {          this.id = id;      }      public String getUserName() {          return userName;      }      public void setUserName(String userName) {          this.userName = userName;      }      public String getUserSex() {          return userSex;      }      public void setUserSex(String userSex) {          this.userSex = userSex;      }    }

4.數據層 dao 和 mapper.xml

public interface UserDao {      //mapper.xml方式      /**       * 獲取所有用戶       * @return       */      List<UserEntity> getAll();      /**       * 根據id獲取用戶       * @return       */      UserEntity getOne(Long id);      /**       * 新增用戶       * @param user       */      void insertUser(UserEntity user);      /**       * 修改用戶       * @param user       */      void updateUser(UserEntity user);      /**       * 刪除用戶       * @param id       */      void deleteUser(Long id);      }
<?xml version="1.0" encoding="UTF-8"?>  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN"  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  <mapper namespace="cn.zwqh.springboot.dao.UserDao">      <resultMap type="cn.zwqh.springboot.model.UserEntity" id="user">          <id property="id" column="id"/>          <result property="userName" column="user_name"/>          <result property="userSex" column="user_sex"/>      </resultMap>      <!-- 獲取所有用戶 -->      <select id="getAll" resultMap="user">          select * from t_user      </select>      <!-- 根據用戶ID獲取用戶 -->      <select id="getOne" resultMap="user">          select * from t_user where id=#{id}      </select>      <!-- 新增用戶 -->      <insert id="insertUser" parameterType="cn.zwqh.springboot.model.UserEntity">          insert into t_user (user_name,user_sex) values(#{userName},#{userSex})      </insert>      <!-- 修改用戶 -->      <update id="updateUser" parameterType="cn.zwqh.springboot.model.UserEntity">          update t_user set user_name=#{userName},user_sex=#{userSex} where id=#{id}      </update>      <!-- 刪除用戶 -->      <delete id="deleteUser" parameterType="Long">          delete from t_user where id=#{id}      </delete>  </mapper>    

5.業務程式碼層介面 Service 和實現類 ServiceImpl

public interface UserService {        /**       * 查找所有       * @return       */      List<UserEntity> getAll();      /**       * 根據id獲取用戶       * @param id       * @return       */      UserEntity getOne(Long id);      /**       * 新增用戶       * @param user       */      void insertUser(UserEntity user);      /**       * 修改用戶       * @param user       */      void updateUser(UserEntity user);        void deleteAll1();        void deleteAll12();  }  
@Service  @CacheConfig(cacheNames = {"userCache"})  public class UserServiceImpl implements UserService {        @Autowired      private UserDao userDao;        @Override      @Cacheable("userList") // 標誌讀取快取操作,如果快取不存在,則調用目標方法,並將結果放入快取      public List<UserEntity> getAll() {          System.out.println("快取不存在,執行方法");          return userDao.getAll();      }        @Override      @Cacheable(cacheNames = { "user" }, key = "#id")//如果快取存在,直接讀取快取值;如果快取不存在,則調用目標方法,並將結果放入快取      public UserEntity getOne(Long id) {          System.out.println("快取不存在,執行方法");          return userDao.getOne(id);      }        @Override      @CachePut(cacheNames = { "user" }, key = "#user.id")//寫入快取,key為user.id,一般該註解標註在新增方法上      public void insertUser(UserEntity user) {          System.out.println("寫入快取");          userDao.insertUser(user);      }        @Override      @CacheEvict(cacheNames = { "user" }, key = "#user.id")//根據key清除快取,一般該註解標註在修改和刪除方法上      public void updateUser(UserEntity user) {          System.out.println("清除快取");          userDao.updateUser(user);      }        @Override      @CacheEvict(value="userCache",allEntries=true)//方法調用後清空所有快取      public void deleteAll1() {        }        @Override      @CacheEvict(value="userCache",beforeInvocation=true)//方法調用前清空所有快取      public void deleteAll2() {        }    }

6.測試 Controller

@RestController  @RequestMapping("/user")  public class UserController {        @Autowired      private UserService userService;        /**       *  查找所有       * @return       */      @RequestMapping("/getAll")      public List<UserEntity> getAll(){          return userService.getAll();      }      /**       * 根據id獲取用戶       * @return       */      @RequestMapping("/getOne")      public UserEntity getOne(Long id){          return userService.getOne(id);      }      /**       * 新增用戶       * @param user       * @return       */      @RequestMapping("/insertUser")      public String insertUser(UserEntity user) {          userService.insertUser(user);          return "insert success";      }      /**       * 修改用戶       * @param user       * @return       */      @RequestMapping("/updateUser")      public String updateUser(UserEntity user) {          userService.updateUser(user);          return "update success";      }  }

7.啟動 Cache 功能

@SpringBootApplication  @MapperScan("cn.zwqh.springboot.dao")  @EnableCaching //啟動 Cache 功能  public class SpringBootCacheApplication {        public static void main(String[] args) {          SpringApplication.run(SpringBootCacheApplication.class, args);      }    }  

8.資料庫及測試數據

資料庫和測試數據仍舊用之前的。

9.測試

編寫單元測試,或者通過訪問 http://127.0.0.1:8080/user/ 加上對應路徑和參數。

文檔

org.springframework.cache

示例程式碼

github

碼雲

非特殊說明,本文版權歸 朝霧輕寒 所有,轉載請註明出處.

原文標題:Spring Boot 2.X(七):Spring Cache 使用

原文地址:https://www.zwqh.top/article/info/13