- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- SpringBoot+SpringCache實(shí)現兩級緩存(Redis+Caffeine)
Spring cache:主要包含spring cache定義的接口方法說(shuō)明和注解中的屬性說(shuō)明
springboot+spring cache:rediscache實(shí)現中的缺陷
caffeine簡(jiǎn)介
spring boot+spring cache實(shí)現兩級緩存
使用緩存時(shí)的流程圖
spring cache是spring-context包中提供的基于注解方式使用的緩存組件,定義了一些標準接口,通過(guò)實(shí)現這些接口,就可以通過(guò)在方法上增加注解來(lái)實(shí)現緩存。這樣就能夠避免緩存代碼與業(yè)務(wù)處理耦合在一起的問(wèn)題。spring cache的實(shí)現是使用spring aop中對方法切面(MethodInterceptor)封裝的擴展,當然spring aop也是基于A(yíng)spect來(lái)實(shí)現的。
spring cache核心的接口就兩個(gè):Cache和CacheManager
1.2.1 Cache接口
提供緩存的具體操作,比如緩存的放入,讀取,清理,spring框架中默認提供的實(shí)現有
1.2.2 CacheManager接口
主要提供Cache實(shí)現bean的創(chuàng )建,每個(gè)應用里可以通過(guò)cacheName來(lái)對Cache進(jìn)行隔離,每個(gè)CaheName對應一個(gè)Cache實(shí)現,spring框架中默認提供的實(shí)現與Cache的實(shí)現都是成對出現的
1.2.3 常用的注解說(shuō)明
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必須要設置cacheLoader //在5分鐘內沒(méi)有創(chuàng )建/覆蓋時(shí),會(huì )移除該key,下次取的時(shí)候從loading中取【重點(diǎn):失效、移除Key、失效后需要獲取新值】 .expireAfterWrite(5, TimeUnit.MINUTES) //初始容量 .initialCapacity(10) //用來(lái)控制cache的最大緩存數量 .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); //設置key的序列化器 template.setKeySerializer(new StringRedisSerializer()); //設置value的序列化器 template.setValueSerializer(new StringRedisSerializer()); return template; } }
一個(gè)使用cacheable注解,一個(gè)使用redistemplate進(jìn)行緩存
因為公司項目中用到的是jedis和jediscluster所以這里只是做個(gè)了解,沒(méi)有寫(xiě)的很細
到此這篇關(guān)于SpringBoot+SpringCache實(shí)現兩級緩存(Redis+Caffeine)的文章就介紹到這了,更多相關(guān)SpringBoot SpringCache兩級緩存內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站