update 优化 redis序列化配置 更改为通用格式(升级需清除redis所有数据)

feature/model
疯狂的狮子Li 1 year ago
parent 15a7a83966
commit 84429206dc

@ -1,12 +1,17 @@
package org.dromara.common.redis.config; package org.dromara.common.redis.config;
import cn.hutool.core.util.ObjectUtil; 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.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.config.properties.RedissonProperties;
import org.dromara.common.redis.handler.KeyPrefixHandler; import org.dromara.common.redis.handler.KeyPrefixHandler;
import org.dromara.common.redis.manager.PlusSpringCacheManager; import org.dromara.common.redis.manager.PlusSpringCacheManager;
import lombok.extern.slf4j.Slf4j; import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonJacksonCodec; import org.redisson.codec.CompositeCodec;
import org.redisson.codec.TypedJsonJacksonCodec;
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer; import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
@ -35,9 +40,16 @@ public class RedisConfig {
@Bean @Bean
public RedissonAutoConfigurationCustomizer redissonCustomizer() { public RedissonAutoConfigurationCustomizer redissonCustomizer() {
return config -> { 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()) config.setThreads(redissonProperties.getThreads())
.setNettyThreads(redissonProperties.getNettyThreads()) .setNettyThreads(redissonProperties.getNettyThreads())
.setCodec(new JsonJacksonCodec(objectMapper)); .setCodec(codec);
RedissonProperties.SingleServerConfig singleServerConfig = redissonProperties.getSingleServerConfig(); RedissonProperties.SingleServerConfig singleServerConfig = redissonProperties.getSingleServerConfig();
if (ObjectUtil.isNotNull(singleServerConfig)) { if (ObjectUtil.isNotNull(singleServerConfig)) {
// 使用单机模式 // 使用单机模式

@ -13,7 +13,6 @@ import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StreamUtils; import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.StringUtils; 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.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo; import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.oss.constant.OssConstant; import org.dromara.common.oss.constant.OssConstant;
@ -66,7 +65,7 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService {
if ("0".equals(config.getStatus())) { if ("0".equals(config.getStatus())) {
RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey); 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 { } finally {
@ -101,7 +100,7 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService {
validEntityBeforeSave(config); validEntityBeforeSave(config);
boolean flag = baseMapper.insert(config) > 0; boolean flag = baseMapper.insert(config) > 0;
if (flag) { if (flag) {
CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), config);
} }
return flag; return flag;
} }
@ -118,7 +117,7 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService {
luw.eq(SysOssConfig::getOssConfigId, config.getOssConfigId()); luw.eq(SysOssConfig::getOssConfigId, config.getOssConfigId());
boolean flag = baseMapper.update(config, luw) > 0; boolean flag = baseMapper.update(config, luw) > 0;
if (flag) { if (flag) {
CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config)); CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), config);
} }
return flag; return flag;
} }

Loading…
Cancel
Save