SpringCache緩存處理詳解
SpringCache緩存處理
學(xué)習(xí)過redis的都知道redis緩存是一個很不錯的nosql,對于上線項(xiàng)目正的有很大的幫助,平常在java中使用一般都是通過 jedis 或者 spring-boot-starter-data-redis 所提供的依賴進(jìn)行開發(fā),具體的api還需要我們手動的去調(diào)用實(shí)現(xiàn),下面的SpringCache將會為我們使用注解的形式來快速使用redis
1、SpringCache介紹
SpringCache是一個框架,實(shí)現(xiàn)了基于注解緩存功能,只需要簡單地加一個注解,就能實(shí)現(xiàn)緩存功能。 SpringCache提高了一層抽象,底層可以切換不同的cache實(shí)現(xiàn),具體就是通過cacheManager接口來統(tǒng)一不同的緩存技術(shù) cacheManager是spring提供的各種緩存技術(shù)抽象接口 針對不同的緩存技術(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項(xiàng)目中,使用緩存技術(shù)只需要在項(xiàng)目中導(dǎo)入相關(guān)的緩存技術(shù)包,并在啟動類上使用@EnableCaching注解開啟緩存技術(shù)支持即可 例如:使用redis作為緩存技術(shù),只需要導(dǎo)入spring-data-redis的maven坐標(biāo)即可
3、SpringCache快速入門
下面幾步的案例都是基于SpringCache底層的Map存儲,當(dāng)服務(wù)器重啟的時候就會清空,在下面第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è)務(wù)更具id修改數(shù)據(jù)。 對于注解的 key 是一個重點(diǎn),詳細(xì)的官方介紹如下
/**
* CachePut:將方法返回的值放入緩存中
* value:緩存的名稱,每一個緩存名稱下面可以有多個key
* key:緩存的key,重點(diǎn)靈活配置
*/
@CachePut(value = "userCache",key = "#users.id")
@GetMapping()
public User get(User user){
User users= dishService.updateById(user);
return users;
}用于動態(tài)計算密鑰的 Spring 表達(dá)式語言 (SpEL) 表達(dá)式。 默認(rèn)為 {@code “”},這意味著所有方法參數(shù)都被視為一個鍵,除非已設(shè)置自定義 {@link keyGenerator}。 SpEL 表達(dá)式根據(jù)提供以下元數(shù)據(jù)的專用上下文進(jìn)行評估: {@code result} 用于對方法調(diào)用結(jié)果的引用。對于 {@code Optional} 等受支持的包裝器,{@code result} 指的是實(shí)際對象,而不是包裝器 {@code root.method}、{@code root.target} 和 {@ code root.caches} 分別用于引用 {@link java.lang.reflect.Method 方法}、目標(biāo)對象和受影響的緩存。 方法名稱的快捷方式({@code root. methodName})和目標(biāo)類({@code root.targetClass})也可用。 方法參數(shù)可以通過索引訪問。例如,可以通過 {@code root.args[1]}、{@code p1} 或 {@code a1} 訪問第二個參數(shù)。如果該信息可用,也可以按名稱訪問參數(shù)。
3.4、沖緩存中刪除數(shù)據(jù)【刪除和修改方法使用】
下面列舉了三種獲取key值的方法,選擇哪一種都可以
/**
* CacheEvict:清理指定緩存
* value:緩存的名稱,每一個緩存名稱下面可以有多個key
* key:緩存的key,重點(diǎn)靈活配置
*/
// @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ù)就添加,同時添加了一個判斷條件,當(dāng)返回值不為空的時候再進(jìn)行添加緩存
/*
* 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、導(dǎo)入maven坐標(biāo)
<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鏈接學(xué)習(xí)
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整合單機(jī)緩存ehcache的實(shí)現(xiàn)
- SpringBoot中整合Ehcache實(shí)現(xiàn)熱點(diǎn)數(shù)據(jù)緩存的詳細(xì)過程
- Spring中的@Cacheable緩存注解詳解
- SpringBoot?Cache?二級緩存的使用
- 詳解如何使用SpringBoot的緩存@Cacheable
- Spring中緩存注解@Cache的使用詳解
- 詳解Springboot @Cacheable 注解(指定緩存位置)
- Spring Cache @Cacheable 緩存在部分Service中不生效的解決辦法
- Springboot使用@Cacheable注解實(shí)現(xiàn)數(shù)據(jù)緩存
- SpringBoot使用Spring?Cache高效處理緩存數(shù)據(jù)
相關(guān)文章
MyBatis-Plus分頁時排序的實(shí)現(xiàn)方法
這篇文章主要介紹了MyBatis-Plus分頁時的排序,分頁時排序的方法,后端OrderItems排序、Wrapper排序前端指定排序,文章結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
SpringBoot框架實(shí)現(xiàn)支付和轉(zhuǎn)賬功能
在 Spring Boot 框架中實(shí)現(xiàn)支付和轉(zhuǎn)賬功能時,涉及到多個細(xì)節(jié)和注意點(diǎn),這些功能通常需要高度的安全性、穩(wěn)定性和可擴(kuò)展性,本文介紹了實(shí)現(xiàn)支付和轉(zhuǎn)賬功能的一些關(guān)鍵點(diǎn),需要的朋友可以參考下2024-08-08

