SpringBoot Redis使用fastjson进行序列化

 在使用spring-data-redis,默认情况下是使用org.springframework.data.redis.serializer.JdkSerializationRedisSerializer这个类来做序列化

我们使用jackson方式:

Jackson redis序列化是spring中自带的

复制代码
    @Bean(name="redisTemplate")
    public RedisTemplate<String, Object> redisTemplate() {
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.setDefaultSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
复制代码

使用fastjson方式:

复制代码
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    private Class<T> clazz;

    public FastJson2JsonRedisSerializer(Class<T> clazz) {
        super();
        this.clazz = clazz;
    }

    public byte[] serialize(T t) throws SerializationException {
        if (t == null) {
            return new byte[0];
        }
        return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }

    public T deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);

        return (T) JSON.parseObject(str, clazz);
    }

}
复制代码

注册:

复制代码
@Configuration
public class RedisConfig {
    @Autowired
    private RedisConnectionFactory factory;

    @Autowired
    private RedisSerializer fastJson2JsonRedisSerializer;

    //fastjson
    @Bean(name="redisTemplate")
    public RedisTemplate<String, Object> fastJsonRedisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        //redis开启事务
        template.setEnableTransactionSupport(true);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(fastJson2JsonRedisSerializer);
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(fastJson2JsonRedisSerializer);
        template.setDefaultSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}
复制代码

在redis工具类中调用RedisTemplate:

@Component
public class RedisCacheUtil {

    @Autowired
    @Qualifier("redisTemplate")
    private  RedisTemplate<String, String> redisTemplate;

}

对比:

jackson方式序列化存储redis中数据:

 [
  "com.qhong.test.dependBean.Person",
  {
    "age": 20,
    "name": "name0",
    "iss": true
  }
]
复制代码
[
  "java.util.ArrayList",
  [
    [
      "com.qhong.test.dependBean.Person",
      {
        "age": 20,
        "name": "name0",
        "iss": true
      }
    ],
    [
      "com.qhong.test.dependBean.Person",
      {
        "age": 21,
        "name": "name1",
        "iss": true
      }
    ],
    [
      "com.qhong.test.dependBean.Person",
      {
        "age": 22,
        "name": "name2",
        "iss": true
      }
    ]
  ]
]
复制代码

上面的完全不符合json格式规范

fastjson方式序列化:

{
  "@type": "com.qhong.test.dependBean.Person",
  "age": 20,
  "iss": true,
  "name": "name0"
}
复制代码
[
  {
    "@type": "com.qhong.test.dependBean.Person",
    "age": 20,
    "iss": true,
    "name": "name0"
  },
  {
    "@type": "com.qhong.test.dependBean.Person",
    "age": 21,
    "iss": true,
    "name": "name1"
  },
  {
    "@type": "com.qhong.test.dependBean.Person",
    "age": 22,
    "iss": true,
    "name": "name2"
  }
]
复制代码

虽然也不是很好,但是比jackson的好多了

 

https://www.jianshu.com/p/138f3713618a

https://github.com/haha174/seckill

posted @   hongdada  阅读(9692)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示