網站首頁 編程語言 正文
目錄:
1.前言
2.常用注解
3.啟用緩存
1.前言
Spring Cache
是Spring提供的一種緩存抽象機制,旨在通過簡化緩存操作來提高系統性能和響應速度。Spring Cache
可以將方法的返回值緩存起來,當下次調用方法時如果從緩存中查詢到了數據,可以直接從緩存中獲取結果,而無需再次執行方法體中的代碼。
2.常用注解
- @Cacheable:在方法執行前查看是否有緩存對應的數據,如果有直接返回數據,如果沒有調用方法獲取數據返回,并緩存起來;
- @CacheEvict:將一條或多條數據從緩存中刪除;
- @CachePut:將方法的返回值放到緩存中;
- @EnableCaching:開啟緩存注解功能;
- @Caching:組合多個緩存注解。
3.啟用緩存
3.1.配置yaml文件
spring:
cache:
type: simple
simple:
time-to-live: 600s
3.2.添加注解
在啟動類上添加注解@EnableCaching
:
@Slf4j
@SpringBootApplication
@EnableCaching
public class SpringBootstrap {
public static void main(String[] args) {
SpringApplication.run(SpringBootstrap.class, args);
}
}
3.3.創建緩存
使用
@CachePut
注解。當方法執行完后,如果緩存不存在則創建緩存;如果緩存存在則更新緩存。注解中的value屬性可指定緩存的名稱,key屬性則可指定緩存的鍵,可使用SpEL表達式來獲取key的值。
這里result表示方法的返回值UserInfo,從UserInfo中獲取id屬性。
@CachePut(value = "user", key = "#result.id")
public UserInfo create(UserCreateRequest request) {
// 將請求中的數據映射給實體類(相關方法自行創建)
User user = UserConverter.createByRequest(request);
boolean save = userService.save(user);
if (save) {
return UserConverter.toInfo(user);
} else {
throw new RuntimeException("User.create.fail");
}
}
3.4.更新緩存
同樣使用
@CachePut
注解。當方法執行完后,如果緩存不存在則創建緩存;如果緩存存在則更新緩存。
@CachePut(value = "user", key = "#result.id")
public UserInfo update(UserUpdateRequest request) {
// 將請求中的數據映映射給wrapper(相關方法自行創建)
Wrapper<User> wrapper = UserConverter.updateWrapperByRequest(request);
boolean update = userService.update(wrapper);
if (update) {
return UserConverter.toInfo(user);
} else {
throw new RuntimeException("User.update.fail");
}
}
3.5.查詢緩存
使用
@Cacheable
注解。在方法執行前,首先會查詢緩存,如果緩存不存在,則根據方法的返回結果創建緩存;如果緩存存在,則直接返回數據,不執行方法。這里使用request表示方法的參數UserQueryRequest。
@Cacheable(value = "user", key = "#request.id")
public UserInfo query(UserQueryRequest request) {
User user = userService.getById(request.getId());
if (Objects.isNull(user)) {
throw new RuntimeException("User.not.exist");
}
return c2cInterestCategory;
}
3.6.刪除緩存
使用
@CacheEvict
注解。當方法執行完后,會根據key刪除對應的緩存。這里可以使用condition屬性,當返回結果為true(刪除成功)后,才去刪除緩存。
@CacheEvict(value = "user", key = "#request.id", condition = "#result.equals(true)")
public Boolean delete(UserDeleteRequest request) {
return userService.removeById(request.getId());
}
3.7.多重操作
使用
@Caching
注解,通過使用不同的屬性進行相應操作。
創建/更新多個緩存:
@Caching(
put = {
@CachePut(value = "userById", key = "#result.id"),
@CachePut(value = "userByPhone", key = "#request.phone")
}
)
刪除多個緩存:
@Caching(
evict = {
@CacheEvict(value = "userById", key = "#result.id"),
@CacheEvict(value = "userByPhone", key = "#request.phone")
}
)
原文鏈接:https://blog.csdn.net/k123456kah/article/details/140445920
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2023-02-15 C++在vscode中的多文件編程問題解讀_C 語言
- 2022-11-03 python回歸分析邏輯斯蒂模型之多分類任務詳解_python
- 2022-06-02 C語言基于EasyX庫實現有圖形界面時鐘_C 語言
- 2022-05-22 Nginx反向代理與負載均衡概念理解及模塊使用_nginx
- 2022-07-03 nginx?緩存使用及配置步驟_nginx
- 2022-08-27 python基礎篇之pandas常用基本函數匯總_python
- 2022-03-08 Asp.NetCore3.1開源項目升級為.Net6.0的方法實現_實用技巧
- 2022-07-02 Prometheus+Grafana監控Docker容器和Linux主機
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支