From 897247075bcb65c75231d43d0f1962be180fb674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Thu, 4 Jan 2024 09:54:43 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=88=9B=E5=BB=BA=20caffeine=20=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dict/service/impl/DictServiceImpl.java | 27 ++++++----- .../redis/config/CacheConfiguration.java | 45 +++++++++++++++++++ .../redis/config/RedisConfiguration.java | 12 ----- .../redis/manager/PlusCacheWrapper.java | 13 ++---- ...ot.autoconfigure.AutoConfiguration.imports | 1 + 5 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/CacheConfiguration.java diff --git a/ruoyi-common/ruoyi-common-dict/src/main/java/org/dromara/common/dict/service/impl/DictServiceImpl.java b/ruoyi-common/ruoyi-common-dict/src/main/java/org/dromara/common/dict/service/impl/DictServiceImpl.java index 51936131..a0a0d2a8 100644 --- a/ruoyi-common/ruoyi-common-dict/src/main/java/org/dromara/common/dict/service/impl/DictServiceImpl.java +++ b/ruoyi-common/ruoyi-common-dict/src/main/java/org/dromara/common/dict/service/impl/DictServiceImpl.java @@ -1,7 +1,6 @@ package org.dromara.common.dict.service.impl; -import cn.dev33.satoken.context.SaHolder; -import cn.hutool.core.util.ObjectUtil; +import com.github.benmanes.caffeine.cache.Cache; import org.apache.dubbo.config.annotation.DubboReference; import org.dromara.common.core.constant.CacheConstants; import org.dromara.common.core.service.DictService; @@ -9,6 +8,7 @@ import org.dromara.common.core.utils.StreamUtils; import org.dromara.common.core.utils.StringUtils; import org.dromara.system.api.RemoteDictService; import org.dromara.system.api.domain.vo.RemoteDictDataVo; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Arrays; @@ -24,6 +24,9 @@ import java.util.stream.Collectors; @Service public class DictServiceImpl implements DictService { + @Autowired + private Cache ceffeine; + @DubboReference private RemoteDictService remoteDictService; @@ -35,15 +38,13 @@ public class DictServiceImpl implements DictService { * @param separator 分隔符 * @return 字典标签 */ + @SuppressWarnings("unchecked") @Override public String getDictLabel(String dictType, String dictValue, String separator) { // 优先从本地缓存获取 - List datas = (List) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType); - if (ObjectUtil.isNull(datas)) { - datas = remoteDictService.selectDictDataByType(dictType); - SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas); - } - + List datas = (List) ceffeine.get(CacheConstants.SYS_DICT_KEY + dictType, k -> { + return remoteDictService.selectDictDataByType(dictType); + }); Map map = StreamUtils.toMap(datas, RemoteDictDataVo::getDictValue, RemoteDictDataVo::getDictLabel); if (StringUtils.containsAny(dictValue, separator)) { return Arrays.stream(dictValue.split(separator)) @@ -62,15 +63,13 @@ public class DictServiceImpl implements DictService { * @param separator 分隔符 * @return 字典值 */ + @SuppressWarnings("unchecked") @Override public String getDictValue(String dictType, String dictLabel, String separator) { // 优先从本地缓存获取 - List datas = (List) SaHolder.getStorage().get(CacheConstants.SYS_DICT_KEY + dictType); - if (ObjectUtil.isNull(datas)) { - datas = remoteDictService.selectDictDataByType(dictType); - SaHolder.getStorage().set(CacheConstants.SYS_DICT_KEY + dictType, datas); - } - + List datas = (List) ceffeine.get(CacheConstants.SYS_DICT_KEY + dictType, k -> { + return remoteDictService.selectDictDataByType(dictType); + }); Map map = StreamUtils.toMap(datas, RemoteDictDataVo::getDictLabel, RemoteDictDataVo::getDictValue); if (StringUtils.containsAny(dictLabel, separator)) { return Arrays.stream(dictLabel.split(separator)) diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/CacheConfiguration.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/CacheConfiguration.java new file mode 100644 index 00000000..06c14b0c --- /dev/null +++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/CacheConfiguration.java @@ -0,0 +1,45 @@ +package org.dromara.common.redis.config; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import org.dromara.common.redis.manager.PlusSpringCacheManager; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; + +import java.util.concurrent.TimeUnit; + +/** + * 缓存配置 + * + * @author Lion Li + */ +@AutoConfiguration +@EnableCaching +public class CacheConfiguration { + + /** + * caffeine 本地缓存处理器 + */ + @Bean + public Cache caffeine() { + return Caffeine.newBuilder() + // 设置最后一次写入或访问后经过固定时间过期 + .expireAfterWrite(30, TimeUnit.SECONDS) + // 初始的缓存空间大小 + .initialCapacity(100) + // 缓存的最大条数 + .maximumSize(1000) + .build(); + } + + /** + * 自定义缓存管理器 整合spring-cache + */ + @Bean + public CacheManager cacheManager() { + return new PlusSpringCacheManager(); + } + +} diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfiguration.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfiguration.java index 8264423d..e4b7b9cf 100644 --- a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfiguration.java +++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfiguration.java @@ -8,7 +8,6 @@ import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator import lombok.extern.slf4j.Slf4j; import org.dromara.common.redis.config.properties.RedissonProperties; import org.dromara.common.redis.handler.KeyPrefixHandler; -import org.dromara.common.redis.manager.PlusSpringCacheManager; import org.redisson.client.codec.StringCodec; import org.redisson.codec.CompositeCodec; import org.redisson.codec.TypedJsonJacksonCodec; @@ -16,8 +15,6 @@ import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; /** @@ -27,7 +24,6 @@ import org.springframework.context.annotation.Bean; */ @Slf4j @AutoConfiguration -@EnableCaching @EnableConfigurationProperties(RedissonProperties.class) public class RedisConfiguration { @@ -86,14 +82,6 @@ public class RedisConfiguration { }; } - /** - * 自定义缓存管理器 整合spring-cache - */ - @Bean - public CacheManager cacheManager() { - return new PlusSpringCacheManager(); - } - /** * redis集群配置 yml * diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusCacheWrapper.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusCacheWrapper.java index 65a3e89f..acedd2c4 100644 --- a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusCacheWrapper.java +++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/manager/PlusCacheWrapper.java @@ -1,11 +1,10 @@ package org.dromara.common.redis.manager; import cn.hutool.core.lang.Console; -import com.github.benmanes.caffeine.cache.Caffeine; +import org.dromara.common.core.utils.SpringUtils; import org.springframework.cache.Cache; import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; /** * Cache 装饰器(用于扩展一级缓存) @@ -14,14 +13,8 @@ import java.util.concurrent.TimeUnit; */ public class PlusCacheWrapper implements Cache { - private static final com.github.benmanes.caffeine.cache.Cache CAFFEINE = Caffeine.newBuilder() - // 设置最后一次写入或访问后经过固定时间过期 - .expireAfterWrite(30, TimeUnit.SECONDS) - // 初始的缓存空间大小 - .initialCapacity(100) - // 缓存的最大条数 - .maximumSize(1000) - .build(); + private static final com.github.benmanes.caffeine.cache.Cache + CAFFEINE = SpringUtils.getBean("caffeine"); private final Cache cache; diff --git a/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 934b5858..506a81fc 100644 --- a/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/ruoyi-common/ruoyi-common-redis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1,2 @@ org.dromara.common.redis.config.RedisConfiguration +org.dromara.common.redis.config.CacheConfiguration