SpringBoot整合Redis緩存的方法:
1、引入緩存依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.1.5.RELEASE</version> </dependency>
2. 增加緩存配置
在application.properties文件中增加以下配置
## Redis部分 # Redis服務(wù)器地址 spring.redis.host=${redis.host} # Redis服務(wù)器連接端口 spring.redis.port=${redis.port} # Redis服務(wù)器連接密碼(默認(rèn)為空) spring.redis.password=${redis.password} # 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) spring.redis.jedis.pool.max-active=${redis.maxTotal} # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制) spring.redis.jedis.pool.max-wait=-1ms # 連接池中的最大空閑連接 spring.redis.jedis.pool.max-idle=${redis.maxIdle} # 連接池中的最小空閑連接 spring.redis.jedis.pool.min-idle=4 # 連接超時(shí)時(shí)間(毫秒) spring.redis.timeout=5000 ## Cache部分 #緩存的名稱集合,多個(gè)采用逗號(hào)分割 spring.cache.cache-names= #緩存的類型,官方提供了很多,這里我們填寫redis spring.cache.type=redis #是否緩存null數(shù)據(jù),默認(rèn)是false spring.cache.redis.cache-null-values=false #redis中緩存超時(shí)的時(shí)間,默認(rèn)60000ms spring.cache.redis.time-to-live=60000 #緩存數(shù)據(jù)key是否使用前綴,默認(rèn)是true spring.cache.redis.use-key-prefix=true #緩存數(shù)據(jù)key的前綴,在上面的配置為true時(shí)有效, spring.cache.redis.key-prefix=
3. 增加開啟緩存注解EnableCaching
@EnableCaching public class WebApplication { public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } }
4. 增加緩存注解
@Cacheable
該注解作用是標(biāo)識(shí)這個(gè)方法返回值將會(huì)被緩存;
需要注意?condition?和?unless?,它們都是條件判斷參數(shù):
condition:在調(diào)用方法之前進(jìn)行判斷,所以不能將方法的結(jié)果值作為判斷條件;
unless:在調(diào)用方法之后進(jìn)行判斷,此時(shí)可以拿到方法放回值作為判斷條件。
所以依賴方法返回值作為是否進(jìn)行緩存的操作必須使用?unless?參數(shù),而不是?condition
@CachePut
將方法返回值更新當(dāng)前緩存
@CacheEvict
將當(dāng)前緩存過期(清空)
更多相關(guān)知識(shí)請關(guān)注redis入門教程欄目
以上就是SpringBoot整合Redis緩存的方法介紹的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
每個(gè)人都需要一臺(tái)速度更快、更穩(wěn)定的 PC。隨著時(shí)間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺(tái)進(jìn)程會(huì)占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://www.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)