SpringCache緩存處理詳解
SpringCache緩存處理
學習過redis的都知道redis緩存是一個很不錯的nosql,對于上線項目正的有很大的幫助,平常在java中使用一般都是通過 jedis 或者 spring-boot-starter-data-redis 所提供的依賴進行開發(fā),具體的api還需要我們手動的去調(diào)用實現(xiàn),下面的SpringCache將會為我們使用注解的形式來快速使用redis
1、SpringCache介紹
SpringCache是一個框架,實現(xiàn)了基于注解緩存功能,只需要簡單地加一個注解,就能實現(xiàn)緩存功能。 SpringCache提高了一層抽象,底層可以切換不同的cache實現(xiàn),具體就是通過cacheManager接口來統(tǒng)一不同的緩存技術(shù) cacheManager是spring提供的各種緩存技術(shù)抽象接口 針對不同的緩存技術(shù)需要實現(xiàn)不同的cacheManager接口
CacheManager | 描述 |
EhCacheCacheManager | 使用EhCache作為緩存技術(shù) |
GuavaCaceManager | 使用Google的GuavaCache作為緩存技術(shù) |
RedisCacheManager | 使用Redis作為緩存技術(shù) |
2、Spring Cache常用注解
注解 | 說明 |
@EnableCaching | 開啟緩存注解功能 |
@Cacheable | 在方法執(zhí)行前spring先查看緩存中是否有數(shù)據(jù),如果有數(shù)據(jù),則直接返回緩存數(shù)據(jù);若沒有數(shù)據(jù),調(diào)用方法并將方法返回值放到緩存中 |
@CachePut | 將方法的返回值放到緩存中 |
@CacheEvict | 將一個或多條數(shù)據(jù)從緩存中刪除 |
在springboot項目中,使用緩存技術(shù)只需要在項目中導入相關(guān)的緩存技術(shù)包,并在啟動類上使用@EnableCaching注解開啟緩存技術(shù)支持即可 例如:使用redis作為緩存技術(shù),只需要導入spring-data-redis的maven坐標即可
3、SpringCache快速入門
下面幾步的案例都是基于SpringCache底層的Map存儲,當服務器重啟的時候就會清空,在下面第4段的時候會講解如何存儲到redis
3.1、在啟動類上開redis注解緩存技術(shù)
import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.cache.annotation.EnableCaching; @Slf4j @SpringBootApplication @ServletComponentScan @EnableCaching public class ReggieApplication { public static void main(String[] args) { SpringApplication.run(ReggieApplication.class); } }
3.2、在控制器中注入CacheManager
/** * 注入注解緩存接口 */ @Autowired private CacheManager cacheManager;
3.3、在具體的方法中使用注解完成緩存添加
這里的控制器使用的是mybatisplus框架提供的接口業(yè)務更具id修改數(shù)據(jù)。 對于注解的 key 是一個重點,詳細的官方介紹如下
/** * CachePut:將方法返回的值放入緩存中 * value:緩存的名稱,每一個緩存名稱下面可以有多個key * key:緩存的key,重點靈活配置 */ @CachePut(value = "userCache",key = "#users.id") @GetMapping() public User get(User user){ User users= dishService.updateById(user); return users; }
用于動態(tài)計算密鑰的 Spring 表達式語言 (SpEL) 表達式。 默認為 {@code “”},這意味著所有方法參數(shù)都被視為一個鍵,除非已設置自定義 {@link keyGenerator}。 SpEL 表達式根據(jù)提供以下元數(shù)據(jù)的專用上下文進行評估: {@code result} 用于對方法調(diào)用結(jié)果的引用。對于 {@code Optional} 等受支持的包裝器,{@code result} 指的是實際對象,而不是包裝器 {@code root.method}、{@code root.target} 和 {@ code root.caches} 分別用于引用 {@link java.lang.reflect.Method 方法}、目標對象和受影響的緩存。 方法名稱的快捷方式({@code root. methodName})和目標類({@code root.targetClass})也可用。 方法參數(shù)可以通過索引訪問。例如,可以通過 {@code root.args[1]}、{@code p1} 或 {@code a1} 訪問第二個參數(shù)。如果該信息可用,也可以按名稱訪問參數(shù)。
3.4、沖緩存中刪除數(shù)據(jù)【刪除和修改方法使用】
下面列舉了三種獲取key值的方法,選擇哪一種都可以
/** * CacheEvict:清理指定緩存 * value:緩存的名稱,每一個緩存名稱下面可以有多個key * key:緩存的key,重點靈活配置 */ // @CacheEvict(value = "userCache",key = "#p0") // @CacheEvict(value = "userCache",key = "#root.args[0]") @CacheEvict(value = "userCache",key = "#id") @DeletMapping("/{id}") public void get(@PathVariable Long id){ dishService.removeById(id); }
3.5、查看緩存數(shù)據(jù),沒有就添加
如果緩存中有數(shù)據(jù)就不執(zhí)行方法直接放回,沒有緩存數(shù)據(jù)就添加,同時添加了一個判斷條件,當返回值不為空的時候再進行添加緩存
/* * unless:滿足條件不緩存 * condition:滿足條件時才緩存 * */ // @CacheEvict(value = "userCache",key = "#p0") // @CacheEvict(value = "userCache",key = "#root.args[0]") @Cacheable(value = "userCache",key = "#users.id",unless= "#result == bull") @GetMapping("/{id}") public User get(@PathVariable Long id){ User users = userService.getById(id); return users; }
4、Spring Cache緩存到Redis中
4.1、導入maven坐標
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--使用注解完成緩存技術(shù)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
這里的操作redis是必須要有 spring-boot-starter-data-redis 依賴包的
4.2、在application.yml配置文件中配置Redis
spring: # 配置redis鏈接學習 redis: host: localhost port: 6379 database: 0 # 操作的庫(有12個) jedis: pool: max-active: 8 # 最大鏈接數(shù)據(jù) max-wait: 1ms # 連接池最大阻塞等待時間 max-idle: 4 # 連接線中最大的空閑鏈接 min-idle: 0 # 連接池中最小空閑鏈接 cache: redis: time-to-live: 1800000 # 緩存的過期時間
到此這篇關(guān)于SpringCache緩存處理詳解的文章就介紹到這了,更多相關(guān)SpringCache緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot整合單機緩存ehcache的實現(xiàn)
- SpringBoot中整合Ehcache實現(xiàn)熱點數(shù)據(jù)緩存的詳細過程
- Spring中的@Cacheable緩存注解詳解
- SpringBoot?Cache?二級緩存的使用
- 詳解如何使用SpringBoot的緩存@Cacheable
- Spring中緩存注解@Cache的使用詳解
- 詳解Springboot @Cacheable 注解(指定緩存位置)
- Spring Cache @Cacheable 緩存在部分Service中不生效的解決辦法
- Springboot使用@Cacheable注解實現(xiàn)數(shù)據(jù)緩存
- SpringBoot使用Spring?Cache高效處理緩存數(shù)據(jù)
相關(guān)文章
SSH框架網(wǎng)上商城項目第20戰(zhàn)之在線支付平臺
這篇文章主要為大家詳細介紹了SSH框架網(wǎng)上商城項目第20戰(zhàn)之在線支付平臺,關(guān)于第三方支付的內(nèi)容從本文開始,感興趣的小伙伴們可以參考一下2016-06-06Springboot 整合 Java DL4J 實現(xiàn)智能客服功能
本文主要介紹了如何使用SpringBoot整合JavaDeeplearning4j來構(gòu)建一個智能客服系統(tǒng),詳細探討了神經(jīng)網(wǎng)絡選擇、數(shù)據(jù)集格式、技術(shù)介紹、Maven依賴、代碼示例等內(nèi)容,為構(gòu)建高效、便捷、個性化的客戶服務提供了理論支持和實踐指導2024-10-10Java中TreeSet、HashSet、Collection重寫比較器的實現(xiàn)
比較器是一種可以對集合或數(shù)組中的元素按照自定義的方式進行排序的對象,本文主要介紹了Java中TreeSet、HashSet、Collection重寫比較器的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-08-08mybatis執(zhí)行錯誤但sql執(zhí)行正常問題
這篇文章主要介紹了mybatis執(zhí)行錯誤但sql執(zhí)行正常問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01springmvc字符編碼過濾器CharacterEncodingFilter的使用
這篇文章主要介紹了springmvc字符編碼過濾器CharacterEncodingFilter的使用,具有很好的參考價值,希望對大家有所幫助。2021-08-08@RequestBody,@RequestParam和@Param的區(qū)別說明
這篇文章主要介紹了@RequestBody,@RequestParam和@Param的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03