SpringBoot+SpringCache實(shí)現(xiàn)兩級(jí)緩存(Redis+Caffeine)
1. 緩存、兩級(jí)緩存
1.1 內(nèi)容說(shuō)明
Spring cache:主要包含spring cache定義的接口方法說(shuō)明和注解中的屬性說(shuō)明
springboot+spring cache:rediscache實(shí)現(xiàn)中的缺陷
caffeine簡(jiǎn)介
spring boot+spring cache實(shí)現(xiàn)兩級(jí)緩存
使用緩存時(shí)的流程圖
1.2 Sping Cache
spring cache是spring-context包中提供的基于注解方式使用的緩存組件,定義了一些標(biāo)準(zhǔn)接口,通過(guò)實(shí)現(xiàn)這些接口,就可以通過(guò)在方法上增加注解來(lái)實(shí)現(xiàn)緩存。這樣就能夠避免緩存代碼與業(yè)務(wù)處理耦合在一起的問(wèn)題。spring cache的實(shí)現(xiàn)是使用spring aop中對(duì)方法切面(MethodInterceptor)封裝的擴(kuò)展,當(dāng)然spring aop也是基于Aspect來(lái)實(shí)現(xiàn)的。
spring cache核心的接口就兩個(gè):Cache和CacheManager
1.2.1 Cache接口
提供緩存的具體操作,比如緩存的放入,讀取,清理,spring框架中默認(rèn)提供的實(shí)現(xiàn)有
1.2.2 CacheManager接口
主要提供Cache實(shí)現(xiàn)bean的創(chuàng)建,每個(gè)應(yīng)用里可以通過(guò)cacheName來(lái)對(duì)Cache進(jìn)行隔離,每個(gè)CaheName對(duì)應(yīng)一個(gè)Cache實(shí)現(xiàn),spring框架中默認(rèn)提供的實(shí)現(xiàn)與Cache的實(shí)現(xiàn)都是成對(duì)出現(xiàn)的
1.2.3 常用的注解說(shuō)明
- @Cacheable:主要應(yīng)用到查詢數(shù)據(jù)的方法上
- @CacheEvict:清除緩存,主要應(yīng)用到刪除數(shù)據(jù)的方法上
- @CachePut:放入緩存,主要用到對(duì)數(shù)據(jù)有更新的方法上
- @Caching:用于在一個(gè)方法上配置多種注解
- @EnableCaching:?jiǎn)⒂胹pring cache緩存,作為總的開(kāi)關(guān),在spring boot的啟動(dòng)類或配置類上需要加入次注解才會(huì)生效
2.實(shí)戰(zhàn)多級(jí)緩存的用法
package com.xfgg.demo.config; import lombok.AllArgsConstructor; import com.github.benmanes.caffeine.cache.Caffeine; import org.springframework.cache.CacheManager; import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration @AllArgsConstructor //把定義的緩存加入到Caffeine中 public class CacheConfig { @Bean public CacheManager cacheManager(){ CaffeineCacheManager cacheManager = new CaffeineCacheManager(); cacheManager.setCaffeine(Caffeine.newBuilder() //使用refreshAfterWrite必須要設(shè)置cacheLoader //在5分鐘內(nèi)沒(méi)有創(chuàng)建/覆蓋時(shí),會(huì)移除該key,下次取的時(shí)候從loading中取【重點(diǎn):失效、移除Key、失效后需要獲取新值】 .expireAfterWrite(5, TimeUnit.MINUTES) //初始容量 .initialCapacity(10) //用來(lái)控制cache的最大緩存數(shù)量 .maximumSize(150) ); return cacheManager; } }
package com.xfgg.demo.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; //生成的redis連接 public class RedisConfig<GenericObjectPoolConfig> { @Value("${spring.redis1.host}") private String host; @Value("${spring.redis1.port}") private Integer port; @Value("${spring.redis1.password}") private String password; @Value("${spring.redis1.database}") private Integer database; @Value("${spring.redis1.lettuce.pool.max-active}") private Integer maxActive; @Value("${spring.redis1.lettuce.pool.max-idle}") private Integer maxIdle; @Value("${spring.redis1.lettuce.pool.max-wait}") private Long maxWait; @Value("${spring.redis1.lettuce.pool.min-idle}") private Integer minIdle; @Bean public RedisStandaloneConfiguration redis1RedisConfig() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); config.setHostName(host); config.setPassword(RedisPassword.of(password)); config.setPort(port); config.setDatabase(database); return config; } //配置序列化器 @Bean public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String,Object>template=new RedisTemplate<>(); //關(guān)聯(lián) template.setConnectionFactory(factory); //設(shè)置key的序列化器 template.setKeySerializer(new StringRedisSerializer()); //設(shè)置value的序列化器 template.setValueSerializer(new StringRedisSerializer()); return template; } }
一個(gè)使用cacheable注解,一個(gè)使用redistemplate進(jìn)行緩存
因?yàn)楣卷?xiàng)目中用到的是jedis和jediscluster所以這里只是做個(gè)了解,沒(méi)有寫的很細(xì)
到此這篇關(guān)于SpringBoot+SpringCache實(shí)現(xiàn)兩級(jí)緩存(Redis+Caffeine)的文章就介紹到這了,更多相關(guān)SpringBoot SpringCache兩級(jí)緩存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC參數(shù)傳遞之基本數(shù)據(jù)類型和復(fù)雜對(duì)象說(shuō)明
這篇文章主要介紹了SpringMVC參數(shù)傳遞之基本數(shù)據(jù)類型和復(fù)雜對(duì)象說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10MyBatis中使用foreach循環(huán)的坑及解決
這篇文章主要介紹了MyBatis中使用foreach循環(huán)的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java調(diào)用打印機(jī)的2種方式舉例(無(wú)驅(qū)/有驅(qū))
我們平時(shí)使用某些軟件或者在超市購(gòu)物的時(shí)候都會(huì)發(fā)現(xiàn)可以使用打印機(jī)進(jìn)行打印,這篇文章主要給大家介紹了關(guān)于Java調(diào)用打印機(jī)的2種方式,分別是無(wú)驅(qū)/有驅(qū)的相關(guān)資料,需要的朋友可以參考下2023-11-11SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延時(shí)隊(duì)列的方案
在很多的業(yè)務(wù)場(chǎng)景中,延時(shí)隊(duì)列可以實(shí)現(xiàn)很多功能,此類業(yè)務(wù)中,一般上是非實(shí)時(shí)的,需要延遲處理的,需要進(jìn)行重試補(bǔ)償?shù)?本文給大家介紹了SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延遲隊(duì)列的方案,文中有詳細(xì)的代碼講解,需要的朋友可以參考下2024-04-04java servlet手機(jī)app訪問(wèn)接口(二)短信驗(yàn)證
這篇文章主要介紹了java servlet手機(jī)app訪問(wèn)接口(二),短信驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12SpringBoot攔截器讀取流后不能再讀取的問(wèn)題
這篇文章主要介紹了SpringBoot攔截器讀取流后不能再讀取的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10淺談MyBatis 如何執(zhí)行一條 SQL語(yǔ)句
Mybatis 是 Java 開(kāi)發(fā)中比較常用的 ORM 框架。在日常工作中,我們都是直接通過(guò) Spring Boot 自動(dòng)配置,并直接使用,但是卻不知道 Mybatis 是如何執(zhí)行一條 SQL 語(yǔ)句的,下面就一起講解一下2021-05-05