国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

瀏覽:67日期:2023-03-14 08:33:03
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í)的流程圖

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

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)有

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

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)用到查詢(xún)數(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)類(lèi)或配置類(lèi)上需要加入次注解才會(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)有寫(xiě)的很細(xì)

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久久久久88色愉愉 | 国产毛片久久精品 | 国产真实生活伦对白 | 亚洲精品一区91 | 114一级毛片免费 | 欧美日韩在线视频一区 | 杨幂精品国产专区91在线 | 亚洲国产成人久久一区久久 | 日韩欧美在线精品 | 男人天堂手机在线 | 久久久久一级片 | 一级一级一片免费高清 | www.91免费视频| 欧美性色xo在线 | 成人免费毛片观看 | 一级毛片私人影院老司机 | 亚洲欧美日韩天堂 | 免费人成年短视频在线观看免费网站 | 天堂8资源8在线 | 精品女厕沟底拍撒尿 | 秘书高跟黑色丝袜国产91在线 | 国产日韩欧美亚洲 | 亚洲女人网 | 在线观看自拍视频 | 成人欧美视频免费看黄黄 | 日鲁夜鲁鲁狠狠综合视频 | 女让张开腿让男人桶视频 | 日韩一级特黄毛片在线看 | 精品久久久久久中文字幕 | 久久国产亚洲精品 | 久久视频在线播放视频99re6 | tom影院亚洲国产日本一区 | 日本一区二区三区高清在线观看 | 精品国产三级a∨在线观看 精品国产三级a在线观看 | 亚洲成a v人片在线观看 | 2021国产成人精品久久 | 欧美69视频在线 | 中文字幕精品一区二区三区视频 | 996re免费热在线视频手机 | 欧美精品hdxxxxx | 99r精品视频|