談談spring-boot-starter-data-redis序列化
阿新 • • 發佈:2020-06-22
在上一篇中[springboot 2.X 整合redis](https://www.cnblogs.com/homeSicker/p/13177730.html)中提到了在spring-boot-starter-data-redis中使用JdkSerializationRedisSerializerl來實現序列化,
這裡看下具體是如何實現的。
1.RedisSerializer介面
在spring-data-redis包下,有一個RedisSerializer介面,提供了序列化和反序列化的基本介面。
```
public interface RedisSerializer {
/**
* 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.
* Note: Ensure that your domain objects are actually {@link java.io.Serializable serializable}.
*
* @return never {@literal null}.
* @since 2.1
*/
static RedisSerializer