Spring-data-redis 序列化方案(一)

JdkSerializationRedisSerializerGenericFastJsonRedisSerializer

源码对比

性能对比——插入

优缺点总结

​ 笔者在上篇中使用了阿里巴巴的fastjsonGenericFastJsonRedisSerializer序列化方案貌似解决了乱码问题,但笔者之后再次排查,发现乱码问题和序列化方案并没有直接关系。想要知道答案,那么请读完此篇文章。

RedisSerializer提供了九种序列化方案,分别为:

Spring-data-redis fastjson
ByteArrayRedisSerializer FastJsonRedisSerializer
GenericJackson2JsonRedisSerializer GenericFastJsonRedisSerializer
GenericToStringSerializer
Jackson2JsonRedisSerializer
JdkSerializationRedisSerializer
OxmSerializer
StringRedisSerializer

Redis在储存数据时需要将数据转化为byte数组进行存储,这一点需要提前说明,接下来这一篇文章只谈 JdkSerializationRedisSerializerGenericFastJsonRedisSerializer这两种序列化方案的相关问题。

源码对比
JdkSerializationRedisSerializer

​ 作为默认的序列化方案,JdkSerializationRedisSerializer 有两个属性:

    private final Converter<Object, byte[]> serializer;
    private final Converter<byte[], Object> deserializer;

​ 想来读者已经猜到了它大致的作用,点进Converter中去看注释的解释:

import org.springframework.lang.Nullable;

/**
 * A converter converts a source object of type {@code S} to a target of type {@code T}.
 *
 * <p>Implementations of this interface are thread-safe and can be shared.
 *
 * <p>Implementations may additionally implement {@link ConditionalConverter}.
 *
 * @author Keith Donald
 * @since 3.0
 * @param <S> the source type
 * @param <T> the target type
 */
@FunctionalInterface
public interface Converter<S, T> {
   

	/**
	 * Convert the source object of type {@code S} to target type {@code T}.
	 * @param source the source object to convert, which must be an instance of {@code S} (never {@code null})
	 * @return the converted object, which must be an instance of {@code T} (potentially {@code null})
	 * @throws IllegalArgumentException if the source cannot be converted to the desired target type
	 */
	@Nullable
	T convert(S source);

}

​ 大致意思就是将S型的源数据转化为T型的数据。所以现在就好理解了,JdkSerializationRedisSerializer 中的两个属性:serializer的作用为将Object转化为byte数组,而deserializer 则相反,将byte数组转化为Object

​ 最重要的就是序列化和反序列化的实现了,且看源码:

    public Object deserialize(@Nullable byte[] bytes) {
   
        if (SerializationUtils.isEmpty(bytes)) {
   
            return null;
        } else {
   
            try {
   
                return this.deserializer
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值