diff --git a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/conf/RedisPluginConfig.java b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/conf/RedisPluginConfig.java index a0fb17db..536ee05b 100644 --- a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/conf/RedisPluginConfig.java +++ b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/conf/RedisPluginConfig.java @@ -19,6 +19,7 @@ import cn.hutool.core.io.IoUtil; import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; +import org.opsli.plugins.redis.jsonserializer.FastJson2JsonRedisSerializer; import org.opsli.plugins.redis.scripts.RedisScriptCache; import org.opsli.plugins.redis.scripts.enums.RedisScriptsEnum; import org.springframework.context.annotation.Bean; @@ -44,7 +45,7 @@ import java.util.List; @Configuration public class RedisPluginConfig { - private static final FastJsonRedisSerializer FAST_JSON_REDIS_SERIALIZER = new FastJsonRedisSerializer<>(Object.class); + private static final FastJson2JsonRedisSerializer FAST_JSON_REDIS_SERIALIZER = new FastJson2JsonRedisSerializer<>(Object.class); @Resource private LettuceConnectionFactory factory; diff --git a/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/jsonserializer/FastJson2JsonRedisSerializer.java b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/jsonserializer/FastJson2JsonRedisSerializer.java new file mode 100644 index 00000000..a5780c4b --- /dev/null +++ b/opsli-plugins/opsli-plugins-redis/src/main/java/org/opsli/plugins/redis/jsonserializer/FastJson2JsonRedisSerializer.java @@ -0,0 +1,90 @@ +package org.opsli.plugins.redis.jsonserializer; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.support.config.FastJsonConfig; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * FastJson2JsonRedisSerializer + * Redis使用FastJson序列化 + * + * @author Parker + * @date 2021年6月1日13:57:02 + */ +@Slf4j +public class FastJson2JsonRedisSerializer implements RedisSerializer { + + public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + private FastJsonConfig fastJsonConfig = new FastJsonConfig(); + private final Class type; + + public FastJson2JsonRedisSerializer(Class type) { + this.type = type; + } + + public FastJsonConfig getFastJsonConfig() { + return fastJsonConfig; + } + + public void setFastJsonConfig(FastJsonConfig fastJsonConfig) { + this.fastJsonConfig = fastJsonConfig; + } + + @Override + public byte[] serialize(T t) throws SerializationException { + if (t == null) { + return new byte[0]; + } + try { + return JSON.toJSONBytesWithFastJsonConfig( + fastJsonConfig.getCharset(), + t, + fastJsonConfig.getSerializeConfig(), + fastJsonConfig.getSerializeFilters(), + fastJsonConfig.getDateFormat(), + JSON.DEFAULT_GENERATE_FEATURE, + fastJsonConfig.getSerializerFeatures() + ); + } catch (Exception ex) { + throw new SerializationException("Could not serialize: " + ex.getMessage(), ex); + } + } + + @Override + public T deserialize(byte[] bytes) throws SerializationException { + if (bytes == null || bytes.length == 0) { + return null; + } + try { + return cast(JSON.parseObject( + bytes, + fastJsonConfig.getCharset(), + type, + fastJsonConfig.getParserConfig(), + fastJsonConfig.getParseProcess(), + JSON.DEFAULT_PARSER_FEATURE, + fastJsonConfig.getFeatures() + )); + } catch (Exception ex) { + // 容错处理 + log.error(ex.getMessage(), ex); + String str = new String(bytes, DEFAULT_CHARSET); + return cast(str); + } + } + + /** + * 欺骗编译器 强制转换 + * @param obj 对象 + * @return T + */ + private T cast(Object obj){ + return (T) obj; + } + +} \ No newline at end of file