From 84429206dc3ef4475ae31eb9e96d00b3c15c7a5b 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, 10 Aug 2023 12:57:42 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20redis=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E9=85=8D=E7=BD=AE=20=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E9=80=9A=E7=94=A8=E6=A0=BC=E5=BC=8F(=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E9=9C=80=E6=B8=85=E9=99=A4redis=E6=89=80=E6=9C=89?= =?UTF-8?q?=E6=95=B0=E6=8D=AE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/redis/config/RedisConfig.java | 18 +++++++++++++++--- .../service/impl/SysOssConfigServiceImpl.java | 7 +++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfig.java b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfig.java index 644edbe0..a271ba46 100644 --- a/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfig.java +++ b/ruoyi-common/ruoyi-common-redis/src/main/java/org/dromara/common/redis/config/RedisConfig.java @@ -1,12 +1,17 @@ package org.dromara.common.redis.config; import cn.hutool.core.util.ObjectUtil; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; +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 lombok.extern.slf4j.Slf4j; -import org.redisson.codec.JsonJacksonCodec; +import org.redisson.client.codec.StringCodec; +import org.redisson.codec.CompositeCodec; +import org.redisson.codec.TypedJsonJacksonCodec; import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfiguration; @@ -35,9 +40,16 @@ public class RedisConfig { @Bean public RedissonAutoConfigurationCustomizer redissonCustomizer() { return config -> { + ObjectMapper om = objectMapper.copy(); + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + // 指定序列化输入的类型,类必须是非final修饰的。序列化时将对象全类名一起保存下来 + om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL); + TypedJsonJacksonCodec jsonCodec = new TypedJsonJacksonCodec(Object.class, om); + // 组合序列化 key 使用 String 内容使用通用 json 格式 + CompositeCodec codec = new CompositeCodec(StringCodec.INSTANCE, jsonCodec, jsonCodec); config.setThreads(redissonProperties.getThreads()) .setNettyThreads(redissonProperties.getNettyThreads()) - .setCodec(new JsonJacksonCodec(objectMapper)); + .setCodec(codec); RedissonProperties.SingleServerConfig singleServerConfig = redissonProperties.getSingleServerConfig(); if (ObjectUtil.isNotNull(singleServerConfig)) { // 使用单机模式 diff --git a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysOssConfigServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysOssConfigServiceImpl.java index 4d421ee0..aca239e0 100644 --- a/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysOssConfigServiceImpl.java +++ b/ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/service/impl/SysOssConfigServiceImpl.java @@ -13,7 +13,6 @@ import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.StreamUtils; import org.dromara.common.core.utils.StringUtils; -import org.dromara.common.json.utils.JsonUtils; import org.dromara.common.mybatis.core.page.PageQuery; import org.dromara.common.mybatis.core.page.TableDataInfo; import org.dromara.common.oss.constant.OssConstant; @@ -66,7 +65,7 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService { if ("0".equals(config.getStatus())) { RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey); } - CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); + CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), config); } } } finally { @@ -101,7 +100,7 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService { validEntityBeforeSave(config); boolean flag = baseMapper.insert(config) > 0; if (flag) { - CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); + CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), config); } return flag; } @@ -118,7 +117,7 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService { luw.eq(SysOssConfig::getOssConfigId, config.getOssConfigId()); boolean flag = baseMapper.update(config, luw) > 0; if (flag) { - CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); + CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), config); } return flag; }