日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot同时集成Redis和Guava作为缓存组件--进一步分析代码

發布時間:2025/3/21 javascript 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot同时集成Redis和Guava作为缓存组件--进一步分析代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

請先看

https://my.oschina.net/u/3866531/blog/1840386

CompositeCacheManager

Composite,混合的,混成的

Spring提供CompositeCacheManager的主要目的就是為了混合使用多種緩存時進行管理。

一、實際測試--CompositeCacheManager中打斷點

斷點打在getCache上

GuavaDataCache源碼--去掉類上的@CacheConfig(cacheManager = "guavaCacheManager")注解

package com.ding.data.cache;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import javax.annotation.PostConstruct;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;@Service//@CacheConfig(cacheManager = "guavaCacheManager")public class GuavaDataCache {private Map<Long, String> dataMap = new HashMap<Long, String>();/*** 初始化*/@PostConstructpublic void init() {dataMap.put(1L, "張三");dataMap.put(2L, "李四");dataMap.put(3L, "王五");}/*** 查詢* 如果數據沒有緩存,那么從dataMap里面獲取,如果緩存了,* 那么從guavaDemo里面獲取* 并且將緩存的數據存入到 guavaDemo里面* 其中key 為 #id+dataMap*/@Cacheable(value="guavaDemo" ,key="#id + 'dataMap'")public String query(Long id) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : query id is " + id);return dataMap.get(id);}/*** 插入 或者更新* 插入或更新數據到dataMap中* 并且緩存到 guavaDemo中* 如果存在了那么更新緩存中的值* 其中key 為 #id+dataMap*/@CachePut(value="guavaDemo" ,key="#id + 'dataMap'")public String put(Long id, String value) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : add data ,id is "+ id);dataMap.put(id, value);// data persistencereturn value;}/*** 刪除* 刪除dataMap里面的數據* 并且刪除緩存guavaDemo中的數據* 其中key 為 #id+dataMap*/@CacheEvict(value="guavaDemo" , key="#id + 'dataMap'")public void remove(Long id) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : remove id is "+ id + " data");dataMap.remove(id);// data remove }}

RedisDataCache源碼--去掉類上的@CacheConfig(cacheManager = "redisCacheManager")注解

package com.ding.data.cache;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import javax.annotation.PostConstruct;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;@Service//@CacheConfig(cacheManager = "redisCacheManager")public class RedisDataCache {private Map<Long, String> dataMap = new HashMap<Long, String>();/*** 初始化*/@PostConstructpublic void init() {dataMap.put(1L, "111");dataMap.put(2L, "222");dataMap.put(3L, "333");}/*** 查詢* 如果數據沒有緩存,那么從dataMap里面獲取,如果緩存了,* 那么從guavaDemo里面獲取* 并且將緩存的數據存入到 guavaDemo里面* 其中key 為 #id+dataMap*/@Cacheable(value="redisDemo" ,key="#id + 'dataMap'")public String query(Long id) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : query id is " + id);return dataMap.get(id);}/*** 插入 或者更新* 插入或更新數據到dataMap中* 并且緩存到 guavaDemo中* 如果存在了那么更新緩存中的值* 其中key 為 #id+dataMap*/@CachePut(value="redisDemo" ,key="#id + 'dataMap'")public String put(Long id, String value) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : add data ,id is "+ id);dataMap.put(id, value);// data persistencereturn value;}/*** 刪除* 刪除dataMap里面的數據* 并且刪除緩存guavaDemo中的數據* 其中key 為 #id+dataMap*/@CacheEvict(value="redisDemo" , key="#id + 'dataMap'")public void remove(Long id) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : remove id is "+ id + " data");dataMap.remove(id);// data remove }}

訪問http://localhost:8080/get?id=1,進入斷點,看到如下

訪問http://localhost:8080/getr?id=1,進入斷點,看到如下

所以可見此時是通過CompositeCacheManager進行管理的,CacheConfig類中的CompositeCacheManager 起作用了

package com.ding.data.config;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.concurrent.TimeUnit;import javax.annotation.Resource;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.cache.CacheManager;import org.springframework.cache.guava.GuavaCacheManager;import org.springframework.cache.support.CompositeCacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;import com.google.common.cache.CacheBuilder;import com.google.common.collect.Lists;@Configurationpublic class CacheConfig implements ApplicationRunner {@Resourceprivate List<CacheManager> cacheManagers;public void run(ApplicationArguments args) throws Exception {System.out.println("CacheManager大小為=========" + cacheManagers.size());System.out.println("=================================================");for(CacheManager c:cacheManagers){System.out.println(c.getCacheNames());}}@Bean(name = "redisCacheManager")public RedisCacheManager redisCacheManager(RedisTemplate<Object, Object> redisTemplate) {redisTemplate.setKeySerializer(new StringRedisSerializer());RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);redisCacheManager.setCacheNames(Arrays.asList("redisDemo"));redisCacheManager.setUsePrefix(true);return redisCacheManager;}@Bean(name = "guavaCacheManager")public GuavaCacheManager getGuavaCacheManager() {GuavaCacheManager guavaCacheManager = new GuavaCacheManager();guavaCacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(3600, TimeUnit.SECONDS).maximumSize(1000));ArrayList<String> guavaCacheNames = Lists.newArrayList();guavaCacheNames.add("guavaDemo");guavaCacheManager.setCacheNames(guavaCacheNames);return guavaCacheManager;}@Bean(name = "cacheManager")@Primarypublic CompositeCacheManager cacheManager(RedisCacheManager redisCacheManager,GuavaCacheManager guavaCacheManager) {CompositeCacheManager cacheManager = new CompositeCacheManager(redisCacheManager, guavaCacheManager);return cacheManager;}}
  • 二、實際測試--CompositeCacheManager中打斷點

斷點打在getCache上

GuavaDataCache源碼--加上類上的@CacheConfig(cacheManager = "guavaCacheManager")注解

package com.ding.data.cache;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import javax.annotation.PostConstruct;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;@Service@CacheConfig(cacheManager = "guavaCacheManager")public class GuavaDataCache {private Map<Long, String> dataMap = new HashMap<Long, String>();/*** 初始化*/@PostConstructpublic void init() {dataMap.put(1L, "張三");dataMap.put(2L, "李四");dataMap.put(3L, "王五");}/*** 查詢* 如果數據沒有緩存,那么從dataMap里面獲取,如果緩存了,* 那么從guavaDemo里面獲取* 并且將緩存的數據存入到 guavaDemo里面* 其中key 為 #id+dataMap*/@Cacheable(value="guavaDemo" ,key="#id + 'dataMap'")public String query(Long id) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : query id is " + id);return dataMap.get(id);}/*** 插入 或者更新* 插入或更新數據到dataMap中* 并且緩存到 guavaDemo中* 如果存在了那么更新緩存中的值* 其中key 為 #id+dataMap*/@CachePut(value="guavaDemo" ,key="#id + 'dataMap'")public String put(Long id, String value) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : add data ,id is "+ id);dataMap.put(id, value);// data persistencereturn value;}/*** 刪除* 刪除dataMap里面的數據* 并且刪除緩存guavaDemo中的數據* 其中key 為 #id+dataMap*/@CacheEvict(value="guavaDemo" , key="#id + 'dataMap'")public void remove(Long id) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : remove id is "+ id + " data");dataMap.remove(id);// data remove }}

RedisDataCache源碼--去掉類上的@CacheConfig(cacheManager = "redisCacheManager")注解

package com.ding.data.cache;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import javax.annotation.PostConstruct;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service;@Service@CacheConfig(cacheManager = "redisCacheManager")public class RedisDataCache {private Map<Long, String> dataMap = new HashMap<Long, String>();/*** 初始化*/@PostConstructpublic void init() {dataMap.put(1L, "111");dataMap.put(2L, "222");dataMap.put(3L, "333");}/*** 查詢* 如果數據沒有緩存,那么從dataMap里面獲取,如果緩存了,* 那么從guavaDemo里面獲取* 并且將緩存的數據存入到 guavaDemo里面* 其中key 為 #id+dataMap*/@Cacheable(value="redisDemo" ,key="#id + 'dataMap'")public String query(Long id) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : query id is " + id);return dataMap.get(id);}/*** 插入 或者更新* 插入或更新數據到dataMap中* 并且緩存到 guavaDemo中* 如果存在了那么更新緩存中的值* 其中key 為 #id+dataMap*/@CachePut(value="redisDemo" ,key="#id + 'dataMap'")public String put(Long id, String value) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : add data ,id is "+ id);dataMap.put(id, value);// data persistencereturn value;}/*** 刪除* 刪除dataMap里面的數據* 并且刪除緩存guavaDemo中的數據* 其中key 為 #id+dataMap*/@CacheEvict(value="redisDemo" , key="#id + 'dataMap'")public void remove(Long id) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()) + " : remove id is "+ id + " data");dataMap.remove(id);// data remove }}

訪問http://localhost:8080/get?id=1,沒進入斷點

訪問http://localhost:8080/getr?id=1,沒進入斷點

所以可見,此時因為標注上了 @CacheConfig(cacheManager = "guavaCacheManager")?和 @CacheConfig(cacheManager = "redisCacheManager")?,所以直接就知道需要用哪個CacheManager了,不需要CompositeCacheManager去匹配了

直接去掉CacheConfig類中的CompositeCacheManager ,在不同的緩存上加上 @CacheConfig(cacheManager = "guavaCacheManager")?和 @CacheConfig(cacheManager = "redisCacheManager"),是否可以?——測試一下

此時的CacheConfig類代碼

package com.ding.data.config;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.concurrent.TimeUnit;import javax.annotation.Resource;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.cache.CacheManager;import org.springframework.cache.guava.GuavaCacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;import com.google.common.cache.CacheBuilder;import com.google.common.collect.Lists;@Configurationpublic class CacheConfig implements ApplicationRunner {@Resourceprivate List<CacheManager> cacheManagers;public void run(ApplicationArguments args) throws Exception {System.out.println("CacheManager大小為=========" + cacheManagers.size());System.out.println("=================================================");for(CacheManager c:cacheManagers){System.out.println(c.getCacheNames());}}@Bean(name = "redisCacheManager")public RedisCacheManager redisCacheManager(RedisTemplate<Object, Object> redisTemplate) {redisTemplate.setKeySerializer(new StringRedisSerializer());RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);redisCacheManager.setCacheNames(Arrays.asList("redisDemo"));redisCacheManager.setUsePrefix(true);return redisCacheManager;}@Bean(name = "guavaCacheManager")public GuavaCacheManager getGuavaCacheManager() {GuavaCacheManager guavaCacheManager = new GuavaCacheManager();guavaCacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(3600, TimeUnit.SECONDS).maximumSize(1000));ArrayList<String> guavaCacheNames = Lists.newArrayList();guavaCacheNames.add("guavaDemo");guavaCacheManager.setCacheNames(guavaCacheNames);return guavaCacheManager;}// @Bean(name = "cacheManager")// @Primary// public CompositeCacheManager cacheManager(// RedisCacheManager redisCacheManager,// GuavaCacheManager guavaCacheManager) {// CompositeCacheManager cacheManager = new CompositeCacheManager(// redisCacheManager, guavaCacheManager);// return cacheManager;// }}

結果啟動時就報錯了

java.lang.IllegalStateException: No CacheResolver specified, and no unique bean of type CacheManager found. Mark one as primary (or give it the name 'cacheManager') or declare a specific CacheManager to use, that serves as the default one.

at org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:186) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:792) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]

at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]

at com.ding.data.App.main(App.java:33) [classes/:na]

上面報錯是因為Spring Context 中存在多個實現了 CacheManager.class 的 Bean,需要使用 @Primary 注解指定優先選擇的 CacheManager

那么指定?GuavaCacheManager?為 @Primary,此時的CacheConfig類代碼

package com.ding.data.config;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.concurrent.TimeUnit;import javax.annotation.Resource;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.cache.CacheManager;import org.springframework.cache.guava.GuavaCacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.StringRedisSerializer;import com.google.common.cache.CacheBuilder;import com.google.common.collect.Lists;@Configurationpublic class CacheConfig implements ApplicationRunner {@Resourceprivate List<CacheManager> cacheManagers;public void run(ApplicationArguments args) throws Exception {System.out.println("CacheManager大小為=========" + cacheManagers.size());System.out.println("=================================================");for(CacheManager c:cacheManagers){System.out.println(c.getCacheNames());}}@Bean(name = "redisCacheManager")public RedisCacheManager redisCacheManager(RedisTemplate<Object, Object> redisTemplate) {redisTemplate.setKeySerializer(new StringRedisSerializer());RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);redisCacheManager.setCacheNames(Arrays.asList("redisDemo"));redisCacheManager.setUsePrefix(true);return redisCacheManager;}@Bean(name = "guavaCacheManager")@Primarypublic GuavaCacheManager getGuavaCacheManager() {GuavaCacheManager guavaCacheManager = new GuavaCacheManager();guavaCacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(3600, TimeUnit.SECONDS).maximumSize(1000));ArrayList<String> guavaCacheNames = Lists.newArrayList();guavaCacheNames.add("guavaDemo");guavaCacheManager.setCacheNames(guavaCacheNames);return guavaCacheManager;}// @Bean(name = "cacheManager")// @Primary// public CompositeCacheManager cacheManager(// RedisCacheManager redisCacheManager,// GuavaCacheManager guavaCacheManager) {// CompositeCacheManager cacheManager = new CompositeCacheManager(// redisCacheManager, guavaCacheManager);// return cacheManager;// }}

正常啟動,剛才的報錯消失

測試,一切正常

所以不需要使用CompositeCacheManager類,在使用緩存的地方通過?@CacheConfig?指定CacheManager,就可以混合使用多種緩存組件了。同時,要記得在配置類中指定一個 @Primary?緩存

轉載于:https://my.oschina.net/u/3866531/blog/1840530

總結

以上是生活随笔為你收集整理的SpringBoot同时集成Redis和Guava作为缓存组件--进一步分析代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。