From 9222d2c42176dce8990923b197c34b7b38a0cc2d Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Mon, 29 Apr 2024 09:10:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=8C=E7=BA=A7=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cacheCtl/service/CacheCtlService.java | 25 +++++++++ .../permission/service/PermissionService.java | 2 - .../resource/service/ResourceService.java | 2 - .../coal/spring/config/CacheConfig.java | 55 +++++++++---------- .../spring/config/MultiLevelCacheManager.java | 6 +- 5 files changed, 52 insertions(+), 38 deletions(-) diff --git a/src/main/java/cn/lihongjie/coal/cacheCtl/service/CacheCtlService.java b/src/main/java/cn/lihongjie/coal/cacheCtl/service/CacheCtlService.java index facb0e30..e720065b 100644 --- a/src/main/java/cn/lihongjie/coal/cacheCtl/service/CacheCtlService.java +++ b/src/main/java/cn/lihongjie/coal/cacheCtl/service/CacheCtlService.java @@ -18,6 +18,7 @@ import com.google.common.base.Splitter; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.redisson.spring.cache.RedissonCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; @@ -29,6 +30,7 @@ import org.springframework.data.redis.cache.RedisCache; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StopWatch; import java.util.ArrayList; import java.util.List; @@ -125,6 +127,20 @@ public class CacheCtlService extends BaseService { + CacheCtlDto dto = new CacheCtlDto(request.getId()); + dto.setId(request.getId()); + dto.setCacheKey(x.toString()); + return dto; + }) + .collect(Collectors.toList()); + + }else { throw new BizException("不支持的缓存类型"); @@ -135,9 +151,16 @@ public class CacheCtlService extends BaseService { @Autowired PermissionRepository repository; diff --git a/src/main/java/cn/lihongjie/coal/resource/service/ResourceService.java b/src/main/java/cn/lihongjie/coal/resource/service/ResourceService.java index d6e06206..9bb2457a 100644 --- a/src/main/java/cn/lihongjie/coal/resource/service/ResourceService.java +++ b/src/main/java/cn/lihongjie/coal/resource/service/ResourceService.java @@ -34,7 +34,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.Cacheable; import org.springframework.context.ApplicationContext; import org.springframework.core.convert.ConversionService; @@ -55,7 +54,6 @@ import java.util.stream.Collectors; @Service @Slf4j @Transactional -@CacheConfig(cacheManager = "caffeineCacheManager") public class ResourceService extends BaseService { @Autowired ResourceRepository repository; diff --git a/src/main/java/cn/lihongjie/coal/spring/config/CacheConfig.java b/src/main/java/cn/lihongjie/coal/spring/config/CacheConfig.java index dc3f63c4..4320b3d3 100644 --- a/src/main/java/cn/lihongjie/coal/spring/config/CacheConfig.java +++ b/src/main/java/cn/lihongjie/coal/spring/config/CacheConfig.java @@ -1,22 +1,19 @@ package cn.lihongjie.coal.spring.config; import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.benmanes.caffeine.cache.CaffeineSpec; -import org.apache.commons.collections.Factory; -import org.apache.commons.collections.map.DefaultedMap; +import org.redisson.api.LocalCachedMapOptions; +import org.redisson.api.RMap; import org.redisson.api.RedissonClient; import org.redisson.spring.cache.RedissonSpringCacheManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.TimeUnit; @EnableCaching @Configuration @@ -26,34 +23,32 @@ public class CacheConfig { @Autowired RedissonClient redissonClient; @Bean - @Qualifier("redisCacheManager" ) - public RedissonSpringCacheManager redisCacheManager() { + @Qualifier("redissonCacheManager") + public RedissonSpringCacheManager redissonCacheManager() { Map cacheConfigs = new HashMap<>(); - return new RedissonSpringCacheManager( - redissonClient, - DefaultedMap.decorate( - cacheConfigs, - new Factory() { - @Override - public Object create() { - return new org.redisson.spring.cache.CacheConfig( - TimeUnit.MINUTES.toMillis(120), - TimeUnit.MINUTES.toMillis(120)); - } - })); + return new RedissonSpringCacheManager(redissonClient, cacheConfigs) { + + @Override + protected RMap getMap( + String name, org.redisson.spring.cache.CacheConfig config) { + + return redissonClient.getLocalCachedMap( + name, + LocalCachedMapOptions.defaults() + .storeMode(LocalCachedMapOptions.StoreMode.LOCALCACHE_REDIS) + .cacheProvider(LocalCachedMapOptions.CacheProvider.CAFFEINE) + .syncStrategy(LocalCachedMapOptions.SyncStrategy.INVALIDATE)); + } + }; } - @Bean - @Qualifier("caffeineCacheManager") - public CaffeineCacheManager caffeineCacheManager() { - CaffeineCacheManager manager = new CaffeineCacheManager(); - manager.setCaffeineSpec(CaffeineSpec.parse("expireAfterWrite=60m")); - return manager; - } - - - - + // @Bean + // @Qualifier("caffeineCacheManager") + // public CaffeineCacheManager caffeineCacheManager() { + // CaffeineCacheManager manager = new CaffeineCacheManager(); + // manager.setCaffeineSpec(CaffeineSpec.parse("expireAfterWrite=60m")); + // return manager; + // } } diff --git a/src/main/java/cn/lihongjie/coal/spring/config/MultiLevelCacheManager.java b/src/main/java/cn/lihongjie/coal/spring/config/MultiLevelCacheManager.java index f84b2383..0c2bfb24 100644 --- a/src/main/java/cn/lihongjie/coal/spring/config/MultiLevelCacheManager.java +++ b/src/main/java/cn/lihongjie/coal/spring/config/MultiLevelCacheManager.java @@ -15,16 +15,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; import org.springframework.cache.caffeine.CaffeineCacheManager; -import org.springframework.context.annotation.Primary; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; -import org.springframework.stereotype.Component; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.*; -@Component -@Primary +//@Component +//@Primary @Slf4j public class MultiLevelCacheManager implements CacheManager {