资讯专栏INFORMATION COLUMN

RedisSerializer之JdkSerializationRedisSerializer分析

Edison / 2200人阅读

摘要:类图结构如上,主要流程如下类实现接口类中和接口。对于,通过定义对象并调用方法对进行反序列化。底层还是通过调用的操作和类实现的序列化和反序列化。

redis在缓存POJO的时候需要将POJO序列化为byte数组进行存储,spring-data-redis实现了类JdkSerializationRedisSerializer对POJO进行序列化。类图结构如上,主要流程如下:

1.JdkSerializationRedisSerializer类实现接口RedisSerializer类中
byte[] serialize(T t)和T deserialize(byte[] bytes)接口。

2.通过定义SerializingConverter和DeserializingConverter对象,对POJO进行序列化和反序列化。

public JdkSerializationRedisSerializer() {
        this(new SerializingConverter(), new DeserializingConverter());
    }
    

3.对于SerializingConverter,通过定义DefaultSerializer对象并调用serialize(Object object, OutputStream outputStream)方法,对POJO进行序列化操作。

    public SerializingConverter() {
            this.serializer = new DefaultSerializer();
        }

    @Override
    public byte[] convert(Object source) {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
        try  {
            this.serializer.serialize(source, byteStream);
            return byteStream.toByteArray();
        }
        catch (Throwable ex) {
            throw new SerializationFailedException("Failed to serialize object using " +
                    this.serializer.getClass().getSimpleName(), ex);
        }
    }
    
    public class DefaultSerializer implements Serializer {
    @Override
    public void serialize(Object object, OutputStream outputStream) throws IOException {
        if (!(object instanceof Serializable)) {
            throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " +
                    "but received an object of type [" + object.getClass().getName() + "]");
        }
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(object);
        objectOutputStream.flush();
    }

}

4.对于DeserializingConverter,通过定义DefaultDeserializer对象并调用deserialize(InputStream inputStream)方法对POJO进行反序列化。

public DeserializingConverter() {
        this.deserializer = new DefaultDeserializer();
    }

@Override
    public Object convert(byte[] source) {
        ByteArrayInputStream byteStream = new ByteArrayInputStream(source);
        try {
            return this.deserializer.deserialize(byteStream);
        }
        catch (Throwable ex) {
            throw new SerializationFailedException("Failed to deserialize payload. " +
                    "Is the byte array a result of corresponding serialization for " +
                    this.deserializer.getClass().getSimpleName() + "?", ex);
        }
    }

public class DefaultDeserializer implements Deserializer {

    private final ClassLoader classLoader;

    @Override
    @SuppressWarnings("resource")
    public Object deserialize(InputStream inputStream) throws IOException {
        ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, this.classLoader);
        try {
            return objectInputStream.readObject();
        }
        catch (ClassNotFoundException ex) {
            throw new NestedIOException("Failed to deserialize object type", ex);
        }
    }

}

5.底层还是通过调用JDK的IO操作ObjectInputStream和ObjectOutputStream类实现POJO的序列化和反序列化。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/61842.html

相关文章

  • RedisSerializerJdkSerializationRedisSerializer分析

    摘要:类图结构如上,主要流程如下类实现接口类中和接口。对于,通过定义对象并调用方法对进行反序列化。底层还是通过调用的操作和类实现的序列化和反序列化。 showImg(https://segmentfault.com/img/bVJxmP?w=938&h=672); redis在缓存POJO的时候需要将POJO序列化为byte数组进行存储,spring-data-redis实现了类JdkSer...

    mtunique 评论0 收藏0
  • RedisSerializerJdkSerializationRedisSerializer分析

    摘要:类图结构如上,主要流程如下类实现接口类中和接口。对于,通过定义对象并调用方法对进行反序列化。底层还是通过调用的操作和类实现的序列化和反序列化。 showImg(https://segmentfault.com/img/bVJxmP?w=938&h=672); redis在缓存POJO的时候需要将POJO序列化为byte数组进行存储,spring-data-redis实现了类JdkSer...

    paulli3 评论0 收藏0
  • RedisSerializerJdkSerializationRedisSerializer分析

    摘要:类图结构如上,主要流程如下类实现接口类中和接口。对于,通过定义对象并调用方法对进行反序列化。底层还是通过调用的操作和类实现的序列化和反序列化。 showImg(https://segmentfault.com/img/bVJxmP?w=938&h=672); redis在缓存POJO的时候需要将POJO序列化为byte数组进行存储,spring-data-redis实现了类JdkSer...

    junnplus 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<