Serializers
序列化转换器
From the framework perspective, the data stored in Redis is just bytes. While Redis itself supports various types, for the most part these refer to the way the data is stored rather than what it represents. It is up to the user to decide whether the information gets translated into Strings or any other objects. The conversion between the user (custom) types and raw data (and vice-versa) is handled in Spring Data Redis through the RedisSerializer interface (package org.springframework.data.redis.serializer) which as the name implies, takes care of the serialization process. Multiple implementations are available out of the box, two of which have been already mentioned before in this documentation: the StringRedisSerializer and the JdkSerializationRedisSerializer. However one can use OxmSerializer for Object/XML mapping through Spring 3 OXM support or either JacksonJsonRedisSerializer, Jackson2JsonRedisSerializer or GenericJackson2JsonRedisSerializer for storing data in JSON format. Do note that the storage format is not limited only to values - it can be used for keys, values or hashes without any restrictions.
从架构层面来看,在Redis中存储的数据是字节类型的。然而Redis它自己支持多种数据类型,在大多数情况下这涉及到数据存储的方式而不是展现的方式。由用户来决定是要转换为Strings还是其他对象objects。用户类型和原生数据类型之间的转换,需要通过Spring Data Redis 的RedisSerializer(包目录位于org.springframework.data.redis.serializer) 接口来实现。该接口关心的是转换的处理过程。多数实现都是有效的,拿来就可以用的;其中的两个在前面的文章中已经提到了:StringRedisSerializer 和 JdkSerializationRedisSerializer。然而还可以使用Spring 3 OXM支持的OxmSerializer 来进行Object与XML转换;也可以使用JacksonJsonRedisSerializer、Jackson2JsonRedisSerializer 或者GenericJackson2JsonRedisSerializer来存储JSON数据类型。注意,存储的类型格式不是只能适用于values,也可以适用于keys、values或者hashes等,其实是没有任何限制的。