谈谈spring-boot-starter-data-redis序列化

在上一篇中springboot 2.X 集成redis中提到了在spring-boot-starter-data-redis中使用JdkSerializationRedisSerializerl来实现序列化
这里看下具体是如何实现的。
1.RedisSerializer接口
在spring-data-redis包下,有一个RedisSerializer接口,提供了序列化和反序列化的基本接口。

public interface RedisSerializer<T> { /** * Serialize the given object to binary data. * * @param t object to serialize. Can be {@literal null}. * @return the equivalent binary data. Can be {@literal null}. */ @Nullable byte[] serialize(@Nullable T t) throws SerializationException; /** * Deserialize an object from the given binary data. * * @param bytes object binary representation. Can be {@literal null}. * @return the equivalent object instance. Can be {@literal null}. */ @Nullable T deserialize(@Nullable byte[] bytes) throws SerializationException; /** * Obtain a {@link RedisSerializer} using java serialization.<br /> * <strong>Note:</strong> Ensure that your domain objects are actually {@link java.io.Serializable serializable}. * * @return never {@literal null}. * @since 2.1 */ static RedisSerializer<Object> java() { return java(null); } /** * Obtain a {@link RedisSerializer} using java serialization with the given {@link ClassLoader}.<br /> * <strong>Note:</strong> Ensure that your domain objects are actually {@link java.io.Serializable serializable}. * * @param classLoader the {@link ClassLoader} to use for deserialization. Can be {@literal null}. * @return new instance of {@link RedisSerializer}. Never {@literal null}. * @since 2.1 */ static RedisSerializer<Object> java(@Nullable ClassLoader classLoader) { return new JdkSerializationRedisSerializer(classLoader); } /** * Obtain a {@link RedisSerializer} that can read and write JSON using * <a href="http://github.com/FasterXML/jackson-core">Jackson</a>. * * @return never {@literal null}. * @since 2.1 */ static RedisSerializer<Object> json() { return new GenericJackson2JsonRedisSerializer(); } /** * Obtain a simple {@link java.lang.String} to {@literal byte[]} (and back) serializer using * {@link java.nio.charset.StandardCharsets#UTF_8 UTF-8} as the default {@link java.nio.charset.Charset}. * * @return never {@literal null}. * @since 2.1 */ static RedisSerializer<String> string() { return StringRedisSerializer.UTF_8; } }

可以看到byte[] serialize(@Nullable T t)和T deserialize(@Nullable byte[] bytes)就是序列化和反序列化接口,并且下面还定义了java的JdkSerializationRedisSerializer序列化、json的GenericJackson2JsonRedisSerializer和string的StringRedisSerializer.UTF_8.
2.1 JdkSerializationRedisSerializer序列化

public class JdkSerializationRedisSerializer implements RedisSerializer<Object> { private final Converter<Object, byte[]> serializer; private final Converter<byte[], Object> deserializer; /** * Creates a new {@link JdkSerializationRedisSerializer} using the default class loader. */ public JdkSerializationRedisSerializer() { this(new SerializingConverter(), new DeserializingConverter()); } /** * Creates a new {@link JdkSerializationRedisSerializer} using a {@link ClassLoader}. * * @param classLoader the {@link ClassLoader} to use for deserialization. Can be {@literal null}. * @since 1.7 */ public JdkSerializationRedisSerializer(@Nullable ClassLoader classLoader) { this(new SerializingConverter(), new DeserializingConverter(classLoader)); } /** * Creates a new {@link JdkSerializationRedisSerializer} using a {@link Converter converters} to serialize and * deserialize objects. * * @param serializer must not be {@literal null} * @param deserializer must not be {@literal null} * @since 1.7 */ public JdkSerializationRedisSerializer(Converter<Object, byte[]> serializer, Converter<byte[], Object> deserializer) { Assert.notNull(serializer, "Serializer must not be null!"); Assert.notNull(deserializer, "Deserializer must not be null!"); this.serializer = serializer; this.deserializer = deserializer; } public Object deserialize(@Nullable byte[] bytes) { if (SerializationUtils.isEmpty(bytes)) { return null; } try { return deserializer.convert(bytes); } catch (Exception ex) { throw new SerializationException("Cannot deserialize", ex); } } @Override public byte[] serialize(@Nullable Object object) { if (object == null) { return SerializationUtils.EMPTY_ARRAY; } try { return serializer.convert(object); } catch (Exception ex) { throw new SerializationException("Cannot serialize", ex); } } }

在JdkSerializationRedisSerializer构造方法中,传入了Converter的两个对象,serialize的序列化就使用SerializingConverter的convert方法

public byte[] convert(Object source) { try { return this.serializer.serializeToByteArray(source); } catch (Throwable ex) { throw new SerializationFailedException("Failed to serialize object using " + this.serializer.getClass().getSimpleName(), ex); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgpdj.html