成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

RedisSerializer之JdkSerializationRedisSerializer分析

Edison / 2346人閱讀

摘要:類(lèi)圖結(jié)構(gòu)如上,主要流程如下類(lèi)實(shí)現(xiàn)接口類(lèi)中和接口。對(duì)于,通過(guò)定義對(duì)象并調(diào)用方法對(duì)進(jìn)行反序列化。底層還是通過(guò)調(diào)用的操作和類(lèi)實(shí)現(xiàn)的序列化和反序列化。

redis在緩存POJO的時(shí)候需要將POJO序列化為byte數(shù)組進(jìn)行存儲(chǔ),spring-data-redis實(shí)現(xiàn)了類(lèi)JdkSerializationRedisSerializer對(duì)POJO進(jìn)行序列化。類(lèi)圖結(jié)構(gòu)如上,主要流程如下:

1.JdkSerializationRedisSerializer類(lèi)實(shí)現(xiàn)接口RedisSerializer類(lèi)中
byte[] serialize(T t)和T deserialize(byte[] bytes)接口。

2.通過(guò)定義SerializingConverter和DeserializingConverter對(duì)象,對(duì)POJO進(jìn)行序列化和反序列化。

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

3.對(duì)于SerializingConverter,通過(guò)定義DefaultSerializer對(duì)象并調(diào)用serialize(Object object, OutputStream outputStream)方法,對(duì)POJO進(jìn)行序列化操作。

    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.對(duì)于DeserializingConverter,通過(guò)定義DefaultDeserializer對(duì)象并調(diào)用deserialize(InputStream inputStream)方法對(duì)POJO進(jìn)行反序列化。

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.底層還是通過(guò)調(diào)用JDK的IO操作ObjectInputStream和ObjectOutputStream類(lèi)實(shí)現(xiàn)POJO的序列化和反序列化。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/61842.html

相關(guān)文章

  • RedisSerializerJdkSerializationRedisSerializer分析

    摘要:類(lèi)圖結(jié)構(gòu)如上,主要流程如下類(lèi)實(shí)現(xiàn)接口類(lèi)中和接口。對(duì)于,通過(guò)定義對(duì)象并調(diào)用方法對(duì)進(jìn)行反序列化。底層還是通過(guò)調(diào)用的操作和類(lèi)實(shí)現(xiàn)的序列化和反序列化。 showImg(https://segmentfault.com/img/bVJxmP?w=938&h=672); redis在緩存POJO的時(shí)候需要將POJO序列化為byte數(shù)組進(jìn)行存儲(chǔ),spring-data-redis實(shí)現(xiàn)了類(lèi)JdkSer...

    mtunique 評(píng)論0 收藏0
  • RedisSerializerJdkSerializationRedisSerializer分析

    摘要:類(lèi)圖結(jié)構(gòu)如上,主要流程如下類(lèi)實(shí)現(xiàn)接口類(lèi)中和接口。對(duì)于,通過(guò)定義對(duì)象并調(diào)用方法對(duì)進(jìn)行反序列化。底層還是通過(guò)調(diào)用的操作和類(lèi)實(shí)現(xiàn)的序列化和反序列化。 showImg(https://segmentfault.com/img/bVJxmP?w=938&h=672); redis在緩存POJO的時(shí)候需要將POJO序列化為byte數(shù)組進(jìn)行存儲(chǔ),spring-data-redis實(shí)現(xiàn)了類(lèi)JdkSer...

    paulli3 評(píng)論0 收藏0
  • RedisSerializerJdkSerializationRedisSerializer分析

    摘要:類(lèi)圖結(jié)構(gòu)如上,主要流程如下類(lèi)實(shí)現(xiàn)接口類(lèi)中和接口。對(duì)于,通過(guò)定義對(duì)象并調(diào)用方法對(duì)進(jìn)行反序列化。底層還是通過(guò)調(diào)用的操作和類(lèi)實(shí)現(xiàn)的序列化和反序列化。 showImg(https://segmentfault.com/img/bVJxmP?w=938&h=672); redis在緩存POJO的時(shí)候需要將POJO序列化為byte數(shù)組進(jìn)行存儲(chǔ),spring-data-redis實(shí)現(xiàn)了類(lèi)JdkSer...

    junnplus 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<