Spring Cache 簡(jiǎn)介
在 Spring 3.1 中引入了多 Cache 的支持,在 spring-context 包中定義了org.springframework.cache.Cache
和 org.springframework.cache.CacheManager
兩個(gè)接口來統(tǒng)一不同的緩存技術(shù)。Cache 接口包含緩存的常用操作:增加、刪除、讀取等。CacheManager 是 Spring 各種緩存的抽象接口。
Spring 支持的常用 CacheManager 如下:
CacheManager | 描述 |
---|---|
SimpleCacheManager | 使用簡(jiǎn)單的 Collection 來存儲(chǔ)緩存 |
ConcurrentMapCacheManager | 使用 java.util.ConcurrentHashMap 來實(shí)現(xiàn)緩存 |
NoOpCacheManager | 僅測(cè)試用,不會(huì)實(shí)際存儲(chǔ)緩存 |
EhCacheCacheManger | 使用EhCache作為緩存技術(shù)。EhCache 是一個(gè)純 Java 的進(jìn)程內(nèi)緩存框架,特點(diǎn)快速、精干,是 Hibernate 中默認(rèn)的 CacheProvider,也是 Java 領(lǐng)域應(yīng)用最為廣泛的緩存 |
JCacheCacheManager | 支持JCache(JSR-107)標(biāo)準(zhǔn)的實(shí)現(xiàn)作為緩存技術(shù) |
CaffeineCacheManager | 使用 Caffeine 作為緩存技術(shù)。用于取代 Guava 緩存技術(shù)。 |
RedisCacheManager | 使用Redis作為緩存技術(shù) |
HazelcastCacheManager | 使用Hazelcast作為緩存技術(shù) |
CompositeCacheManager | 用于組合 CacheManager,可以從多個(gè) CacheManager 中輪詢得到相應(yīng)的緩存 |
Spring Cache 提供了 @Cacheable 、@CachePut 、@CacheEvict 、@Caching 等注解,在方法上使用。通過注解 Cache 可以實(shí)現(xiàn)類似事務(wù)一樣、緩存邏輯透明的應(yīng)用到我們的業(yè)務(wù)代碼上,且只需要更少的代碼。
核心思想:當(dāng)我們調(diào)用一個(gè)方法時(shí)會(huì)把該方法的參數(shù)和返回結(jié)果最為一個(gè)鍵值對(duì)存放在緩存中,等下次利用同樣的參數(shù)來調(diào)用該方法時(shí)將不會(huì)再執(zhí)行,而是直接從緩存中獲取結(jié)果進(jìn)行返回。
Cache注解
1.@EnableCaching
開啟緩存功能,一般放在啟動(dòng)類上。
2.@CacheConfig
當(dāng)我們需要緩存的地方越來越多,你可以使用@CacheConfig(cacheNames = {"cacheName"})注解在 class 之上來統(tǒng)一指定value的值,這時(shí)可省略value,如果你在你的方法依舊寫上了value,那么依然以方法的value值為準(zhǔn)。
3.@Cacheable
根據(jù)方法對(duì)其返回結(jié)果進(jìn)行緩存,下次請(qǐng)求時(shí),如果緩存存在,則直接讀取緩存數(shù)據(jù)返回;如果緩存不存在,則執(zhí)行方法,并把返回的結(jié)果存入緩存中。一般用在查詢方法上。
查看源碼,屬性值如下:
屬性/方法名 | 解釋 |
---|---|
value | 緩存名,必填,它指定了你的緩存存放在哪塊命名空間 |
cacheNames | 與 value 差不多,二選一即可 |
key | 可選屬性,可以使用 SpEL 標(biāo)簽自定義緩存的key |
keyGenerator | key的生成器。key/keyGenerator二選一使用 |
cacheManager | 指定緩存管理器 |
cacheResolver | 指定獲取解析器 |
condition | 條件符合則緩存 |
unless | 條件符合則不緩存 |
sync | 是否使用異步模式,默認(rèn)為false |
4.@CachePut
使用該注解標(biāo)志的方法,每次都會(huì)執(zhí)行,并將結(jié)果存入指定的緩存中。其他方法可以直接從響應(yīng)的緩存中讀取緩存數(shù)據(jù),而不需要再去查詢數(shù)據(jù)庫(kù)。一般用在新增方法上。
查看源碼,屬性值如下:
屬性/方法名 | 解釋 |
---|---|
value | 緩存名,必填,它指定了你的緩存存放在哪塊命名空間 |
cacheNames | 與 value 差不多,二選一即可 |
key | 可選屬性,可以使用 SpEL 標(biāo)簽自定義緩存的key |
keyGenerator | key的生成器。key/keyGenerator二選一使用 |
cacheManager | 指定緩存管理器 |
cacheResolver | 指定獲取解析器 |
condition | 條件符合則緩存 |
unless | 條件符合則不緩存 |
5.@CacheEvict
使用該注解標(biāo)志的方法,會(huì)清空指定的緩存。一般用在更新或者刪除方法上
查看源碼,屬性值如下:
屬性/方法名 | 解釋 |
---|---|
value | 緩存名,必填,它指定了你的緩存存放在哪塊命名空間 |
cacheNames | 與 value 差不多,二選一即可 |
key | 可選屬性,可以使用 SpEL 標(biāo)簽自定義緩存的key |
keyGenerator | key的生成器。key/keyGenerator二選一使用 |
cacheManager | 指定緩存管理器 |
cacheResolver | 指定獲取解析器 |
condition | 條件符合則緩存 |
allEntries | 是否清空所有緩存,默認(rèn)為 false。如果指定為 true,則方法調(diào)用后將立即清空所有的緩存 |
beforeInvocation | 是否在方法執(zhí)行前就清空,默認(rèn)為 false。如果指定為 true,則在方法執(zhí)行前就會(huì)清空緩存 |
6.@Caching
該注解可以實(shí)現(xiàn)同一個(gè)方法上同時(shí)使用多種注解。可從其源碼看出:
public @interface Caching {
Cacheable[] cacheable() default {};
CachePut[] put() default {};
CacheEvict[] evict() default {};
}
Spring Cache 使用
1.構(gòu)建項(xiàng)目,添加依賴
<?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> <!-- 這個(gè)需要為 true 熱部署才有效 -->
</dependency>
<!-- mysql 數(shù)據(jù)庫(kù)驅(qū)動(dòng). -->
<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.實(shí)體類
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.數(shù)據(jù)層 dao 和 mapper.xml
public interface UserDao {
//mapper.xml方式
/**
* 獲取所有用戶
* @return
*/
List<UserEntity> getAll();
/**
* 根據(jù)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>
<!-- 根據(jù)用戶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.業(yè)務(wù)代碼層接口 Service 和實(shí)現(xiàn)類 ServiceImpl
public interface UserService {
/**
* 查找所有
* @return
*/
List<UserEntity> getAll();
/**
* 根據(jù)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") // 標(biāo)志讀取緩存操作,如果緩存不存在,則調(diào)用目標(biāo)方法,并將結(jié)果放入緩存
public List<UserEntity> getAll() {
System.out.println("緩存不存在,執(zhí)行方法");
return userDao.getAll();
}
@Override
@Cacheable(cacheNames = { "user" }, key = "#id")//如果緩存存在,直接讀取緩存值;如果緩存不存在,則調(diào)用目標(biāo)方法,并將結(jié)果放入緩存
public UserEntity getOne(Long id) {
System.out.println("緩存不存在,執(zhí)行方法");
return userDao.getOne(id);
}
@Override
@CachePut(cacheNames = { "user" }, key = "#user.id")//寫入緩存,key為user.id,一般該注解標(biāo)注在新增方法上
public void insertUser(UserEntity user) {
System.out.println("寫入緩存");
userDao.insertUser(user);
}
@Override
@CacheEvict(cacheNames = { "user" }, key = "#user.id")//根據(jù)key清除緩存,一般該注解標(biāo)注在修改和刪除方法上
public void updateUser(UserEntity user) {
System.out.println("清除緩存");
userDao.updateUser(user);
}
@Override
@CacheEvict(value="userCache",allEntries=true)//方法調(diào)用后清空所有緩存
public void deleteAll1() {
}
@Override
@CacheEvict(value="userCache",beforeInvocation=true)//方法調(diào)用前清空所有緩存
public void deleteAll2() {
}
}
6.測(cè)試 Controller
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 查找所有
* @return
*/
@RequestMapping("/getAll")
public List<UserEntity> getAll(){
return userService.getAll();
}
/**
* 根據(jù)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.啟動(dòng) Cache 功能
@SpringBootApplication
@MapperScan("cn.zwqh.springboot.dao")
@EnableCaching //啟動(dòng) Cache 功能
public class SpringBootCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootCacheApplication.class, args);
}
}
8.數(shù)據(jù)庫(kù)及測(cè)試數(shù)據(jù)
數(shù)據(jù)庫(kù)和測(cè)試數(shù)據(jù)仍舊用之前的。
9.測(cè)試
編寫單元測(cè)試,或者通過訪問 http://127.0.0.1:8080/user/
加上對(duì)應(yīng)路徑和參數(shù)。
文檔
示例代碼
非特殊說明,本文版權(quán)歸 朝霧輕寒 所有,轉(zhuǎn)載請(qǐng)注明出處.
原文標(biāo)題:Spring Boot 2.X(七):Spring Cache 使用