Spring Cache
1、Spring Cache介紹
- Spring Cache是一個框架,實現了基于注解的緩存功能,只需要簡單地加一個注解,就能實現緩存功能.
- Spring Cache提供了一層抽象,底層可以切換不同的cache實現。具體就是通過CacheManager接口來統(tǒng)一不同的緩存技術。CacheManager是Spring提供的各種緩存技術抽象接口。
針對不同的緩存技術需要實現不同的CacheManager:
CacheManager | 描述 |
---|---|
EhCacheCacHManager | 使用EhCache作為緩存技術 |
GuavaCacheManager | 使用Google的GuavaCache作為緩存技術 |
RedisCacheManager | 使用Redis作為緩存技術 |
2、Spring Cache常用注解
注解 | 說明 |
---|---|
@EnableCaching | 開啟緩存注解功能 |
@Cacheable | 在方法執(zhí)行前spring先查看緩存中是否有數據,如果有數據,則直接返回緩存數據;若沒有數據,調用方法并將方法返回值放到緩存中 |
@CachePut | 將方法的返回值放到緩存中 |
@cacheEvict | 將一條或多條數據從緩存中刪除 |
在spring boot項目中,使用緩存技術只需在項目中導入相關緩存技術的依賴包,并在啟動類上使用@EnableCaching開啟緩存支持即可。
例如,使用Redis作為緩存技術,只需要導入Spring data Redis的maven坐標即可。
-
我們在使用簡單的緩存技術的時候不用單獨映入相應的依賴,使用spring-boot-starter-web這個包即可,但是要使用redis作為緩存就需要額外的引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
2.1、EnableCaching注解
在啟動類上加上注解@EnableCaching
2.2、CachePut注解
@Autowired
private CacheManager cacheManager;
@Autowired
private UserService userService;
/**
* CachePut:將方法的返回值放入緩存
* value:緩存名稱 每個緩存可以有多個key
* key:緩存的key #result.id:動態(tài)生成keyName進行緩存
*
*
*/
@CachePut(value = "userCache",key="#result.id")
// @CachePut(value = "userCache",key="#user.name") //將user->name作為緩存名
// @CachePut(value = "userCache",key="#user.id") //將user->id作為緩存名
@PostMapping
public User save(User user){
userService.save(user);
return user;
}
key值參數引用(SpEL動態(tài)獲取)的說明:
該注解在沒有緩存配置(如redis)的時候,會將數據緩存在ConcurrentMap里面,但是此緩存操作不具有持久化的儲存,在服務重啟之后會數據丟失。解決方法是使用Redis等緩存中間件。
2.3、CacheEvict注解
- DELETE
/**
* CacheEvict:清理指定緩存
* value:緩存名稱 每個緩存可以有多個key
* key:緩存的key #result.id:動態(tài)生成keyName進行緩存
*/
@CacheEvict(value = "userCache",key = "#id")
//@CacheEvict(value = "userCache",key = "#p0")
//@CacheEvict(value = "userCache",key = "#root.args[0]")
// 上述三種方式都是相同的效果,動態(tài)獲取id的參數作為緩存的唯一標識
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id){
userService.removeById(id);
}
- UPDATE
@CacheEvict(value = "userCache",key = "#user.id")
//@CacheEvict(value = "userCache",key = "#p0.id")
//@CacheEvict(value = "userCache",key = "#root.args[0].id")
//@CacheEvict(value = "userCache",key = "#result.id")
@PutMapping
public User update(User user){
userService.updateById(user);
return user;
}
2.4、Cacheable注解
/**
* Cacheable:在方法執(zhí)行前spring先查看緩存中是否有數據,
* 如果有數據,則直接返回緩存數據;若沒有數據,調用方法并將方法返回值放到緩存中;
* value:緩存名稱 每個緩存可以有多個key
* key:緩存的key #result.id:動態(tài)生成keyName進行緩存
* condition:滿足條件就進行緩存,無法使用#result
* unless:滿足條件就不進行緩存,可以使用#result-->看源碼注解
*/
@Cacheable(value = "userCache", key = "#id", condition = "#unless == null")
@GetMapping("/{id}")
public User getById(@PathVariable Long id) {
User user = userService.getById(id);
return user;
}
在方法執(zhí)行前spring先查看緩存中是否有數據,如果有數據,則直接返回緩存數據;若沒有數據,調用方法并將方法返回值放到緩存中。
@Cacheable(value = "userCache", key = "#user.id+'_'+#user.name")
//多個變量的拼接
@GetMapping("/list")
public List<User> list(User user) {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(user.getId() != null, User::getId, user.getId());
queryWrapper.eq(user.getName() != null, User::getName, user.getName());
List<User> list = userService.list(queryWrapper);
return list;
}
3、Spring Cache使用方式–redis
在Spring Boot項目中使用Spring Cache的操作步驟(使用redis緩存技術):
- 1、導入maven坐標
spring-boot-starter-data-redis
、spring-boot-starter-cache
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置application.yml
spring:
redis:
host: 127.0.0.1
port: 6379
password: 123456
database: 0
cache:
redis:
time-to-live: 1800000 #設置緩存過期時間,可選
3、在啟動類上加入@EnableCaching注解,開啟緩存注解功能
4、在Controller的方法上加入@Cacheable、@CacheEvict等注解,進行緩存操作文章來源:http://www.zghlxwxcb.cn/news/detail-676404.html
Demo項目源碼:SpringCache-Demo文章來源地址http://www.zghlxwxcb.cn/news/detail-676404.html
到了這里,關于Spring Cache的介紹以及怎么使用(redis)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!