色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot+SpringCache實(shí)現(xiàn)兩級緩存(Redis+Caffeine)

瀏覽:78日期:2023-03-14 08:33:03
1. 緩存、兩級緩存1.1 內(nèi)容說明

Spring cache:主要包含spring cache定義的接口方法說明和注解中的屬性說明springboot+spring cache:rediscache實(shí)現(xiàn)中的缺陷caffeine簡介spring boot+spring cache實(shí)現(xiàn)兩級緩存

使用緩存時(shí)的流程圖

SpringBoot+SpringCache實(shí)現(xiàn)兩級緩存(Redis+Caffeine)

1.2 Sping Cache

spring cache是spring-context包中提供的基于注解方式使用的緩存組件,定義了一些標(biāo)準(zhǔn)接口,通過實(shí)現(xiàn)這些接口,就可以通過在方法上增加注解來實(shí)現(xiàn)緩存。這樣就能夠避免緩存代碼與業(yè)務(wù)處理耦合在一起的問題。spring cache的實(shí)現(xiàn)是使用spring aop中對方法切面(MethodInterceptor)封裝的擴(kuò)展,當(dāng)然spring aop也是基于Aspect來實(shí)現(xiàn)的。spring cache核心的接口就兩個(gè):Cache和CacheManager

1.2.1 Cache接口

提供緩存的具體操作,比如緩存的放入,讀取,清理,spring框架中默認(rèn)提供的實(shí)現(xiàn)有

SpringBoot+SpringCache實(shí)現(xiàn)兩級緩存(Redis+Caffeine)

1.2.2 CacheManager接口

主要提供Cache實(shí)現(xiàn)bean的創(chuàng)建,每個(gè)應(yīng)用里可以通過cacheName來對Cache進(jìn)行隔離,每個(gè)CaheName對應(yīng)一個(gè)Cache實(shí)現(xiàn),spring框架中默認(rèn)提供的實(shí)現(xiàn)與Cache的實(shí)現(xiàn)都是成對出現(xiàn)的

1.2.3 常用的注解說明

@Cacheable:主要應(yīng)用到查詢數(shù)據(jù)的方法上 @CacheEvict:清除緩存,主要應(yīng)用到刪除數(shù)據(jù)的方法上 @CachePut:放入緩存,主要用到對數(shù)據(jù)有更新的方法上 @Caching:用于在一個(gè)方法上配置多種注解 @EnableCaching:啟用spring cache緩存,作為總的開關(guān),在spring boot的啟動(dòng)類或配置類上需要加入次注解才會(huì)生效 2.實(shí)戰(zhàn)多級緩存的用法

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)沒有創(chuàng)建/覆蓋時(shí),會(huì)移除該key,下次取的時(shí)候從loading中取【重點(diǎn):失效、移除Key、失效后需要獲取新值】.expireAfterWrite(5, TimeUnit.MINUTES)//初始容量.initialCapacity(10)//用來控制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è)了解,沒有寫的很細(xì)

到此這篇關(guān)于SpringBoot+SpringCache實(shí)現(xiàn)兩級緩存(Redis+Caffeine)的文章就介紹到這了,更多相關(guān)SpringBoot SpringCache兩級緩存內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 免费观看一级特黄欧美大片 | 亚洲精品高清国产一久久 | www.日本在线 | 国产亚洲在线 | 久久亚洲人成国产精品 | 狠狠综合久久久久综合小说网 | 亚洲欧美一区在线 | 视频一区 在线 | 国产女人伦码一区二区三区不卡 | 欧美成人私人视频88在线观看 | 男人天堂亚洲 | 一级毛片在线免费视频 | 精品国产网站 | 亚洲乱码一区二区三区国产精品 | 国产日韩一区二区三区在线播放 | 福利视频美女国产精品 | 成人一级片在线观看 | 日本一区毛片免费观看 | 久久99国产精品久久99 | 九九视频只有精品六 | 亚洲欧美在线不卡 | 久香草视频在线观看免费 | 国内精品1区1区3区4区 | 国产资源免费 | 日韩在线国产 | 欧美成人艳星在线播放 | 看一级特黄a大片日本片 | 色女生影院 | 性盈盈影院67194 | 欧美成性色 | 一区二区三区国产 | 成年女人免费又黄又爽视频 | 国内精品久久久久久影院8f | 成人在线视频国产 | 99久久久免费精品免费 | 免费人成在线观看网站品爱网 | aaa级大片 | 日韩免费一级a毛片在线播放一级 | 最新毛片久热97免费精品视频 | 亚洲激情视频网站 | 亚洲成人免费网址 |