国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

RedisSerializer之JdkSerializationRedisSerializer分析

junnplus / 3774人閱讀

摘要:類圖結構如上,主要流程如下類實現接口類中和接口。對于,通過定義對象并調用方法對進行反序列化。底層還是通過調用的操作和類實現的序列化和反序列化。

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的序列化和反序列化。

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/66690.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...

    Edison 評論0 收藏0
  • RedisSerializerJdkSerializationRedisSerializer分析

    摘要:類圖結構如上,主要流程如下類實現接口類中和接口。對于,通過定義對象并調用方法對進行反序列化。底層還是通過調用的操作和類實現的序列化和反序列化。 showImg(https://segmentfault.com/img/bVJxmP?w=938&h=672); redis在緩存POJO的時候需要將POJO序列化為byte數組進行存儲,spring-data-redis實現了類JdkSer...

    paulli3 評論0 收藏0

發表評論

0條評論

junnplus

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<