Spring Cache 簡介
在 Spring 3.1 中引入了多 Cache 的支援,在 spring-context 包中定義了
org.springframework.cache.Cache
和
org.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
使用該注解标志的方法,每次都會執行,并将結果存入指定的緩存中。其他方法可以直接從響應的緩存中讀取緩存資料,而不需要再去查詢資料庫。一般用在新增方法上。
5.@CacheEvict
使用該注解标志的方法,會清空指定的緩存。一般用在更新或者删除方法上
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如果文章對您有幫助,請掃碼關注下我的公衆号,文章持續更新中...