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

您的位置:首頁技術文章
文章詳情頁

SpringBoot+SpringCache實現兩級緩存(Redis+Caffeine)

瀏覽:95日期:2023-03-14 08:33:03
1. 緩存、兩級緩存1.1 內容說明

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

使用緩存時的流程圖

SpringBoot+SpringCache實現兩級緩存(Redis+Caffeine)

1.2 Sping Cache

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

1.2.1 Cache接口

提供緩存的具體操作,比如緩存的放入,讀取,清理,spring框架中默認提供的實現有

SpringBoot+SpringCache實現兩級緩存(Redis+Caffeine)

1.2.2 CacheManager接口

主要提供Cache實現bean的創建,每個應用里可以通過cacheName來對Cache進行隔離,每個CaheName對應一個Cache實現,spring框架中默認提供的實現與Cache的實現都是成對出現的

1.2.3 常用的注解說明

@Cacheable:主要應用到查詢數據的方法上 @CacheEvict:清除緩存,主要應用到刪除數據的方法上 @CachePut:放入緩存,主要用到對數據有更新的方法上 @Caching:用于在一個方法上配置多種注解 @EnableCaching:啟用spring cache緩存,作為總的開關,在spring boot的啟動類或配置類上需要加入次注解才會生效 2.實戰多級緩存的用法

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分鐘內沒有創建/覆蓋時,會移除該key,下次取的時候從loading中取【重點:失效、移除Key、失效后需要獲取新值】.expireAfterWrite(5, TimeUnit.MINUTES)//初始容量.initialCapacity(10)//用來控制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<>();//關聯template.setConnectionFactory(factory);//設置key的序列化器template.setKeySerializer(new StringRedisSerializer());//設置value的序列化器template.setValueSerializer(new StringRedisSerializer());return template; }}

一個使用cacheable注解,一個使用redistemplate進行緩存因為公司項目中用到的是jedis和jediscluster所以這里只是做個了解,沒有寫的很細

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 午夜国产亚洲精品一区 | 自拍视频网 | 99精品高清不卡在线观看 | 欧美成人tv在线观看免费 | 国产三级一区二区 | 精品一区二区三区的国产在线观看 | 久久视频6免费观看视频精品 | 日韩精品一区二区三区四区 | www.久草视频| 亚洲国产精品大秀在线播放 | 久久免费99精品久久久久久 | 杨幂精品国产专区91在线 | 成年人免费视频网站 | 色多多香蕉| 国产在线观看网址在线视频 | 精品视频免费在线 | 美女黄频免费观看 | 99在线视频精品 | chinese多姿势videos | 日本不卡不码高清免费观看 | 网禁呦萝资源网站在线观看 | 亚洲欧美日韩中文字幕在线一区 | 综合久久一区二区三区 | 91热国内精品永久免费观看 | 日本一视频一区视频二区 | 久9视频这里只有精品 | 久久国产精品永久免费网站 | 欧美日韩精品高清一区二区 | 国产手机在线视频 | 三区在线视频 | 日韩精品一区二区三区在线观看 | 一级欧美一级日韩 | 亚洲成人免费在线观看 | 视频三区精品中文字幕 | 欧美精品亚洲精品日韩一区 | 国产欧美日韩中文久久 | 国产精品毛片一区 | 成人午夜久久 | 在线观看免费亚洲 | 国产毛片在线高清视频 | 国产欧美视频综合二区 |