spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是非常便捷。sdr提供了4种内置的serializer:
- JdkSerializationRedisSerializer:使用JDK的序列化手段(serializable接口,ObjectInputStrean,ObjectOutputStream),数据以字节流存储
- StringRedisSerializer:字符串编码,数据以string存储
- JacksonJsonRedisSerializer:json格式存储
- OxmSerializer:xml格式存储
其中JdkSerializationRedisSerializer和StringRedisSerializer是最基础的序列化策略,其中“JacksonJsonRedisSerializer”与“OxmSerializer”都是基于stirng存储,因此它们是较为“高级”的序列化(最终还是使用string解析以及构建java对象)。
RedisTemplate中需要声明4种serializer,默认为“JdkSerializationRedisSerializer”:
1) keySerializer :对于普通K-V操作时,key采取的序列化策略
2) valueSerializer:value采取的序列化策略
3) hashKeySerializer: 在hash数据结构中,hash-key的序列化策略
4) hashValueSerializer:hash-value的序列化策略
无论如何,建议key/hashKey采用StringRedisSerializer。
接下来,通过实例描述如何使用它们,可以首先参考“spring-data-redis特性”:
一. JdkSerializationRedisSerializer/StringRedisSerializer
1) spring配置文件
- <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="jedisConnectionFactory"></property>
- <property name="keySerializer">
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- <property name="hashKeySerializer">
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- <property name="valueSerializer">
- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
- </property>
- <property name="hashValueSerializer">
- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
- </property>
- </bean>
2) 程序实例:
- ValueOperations<String, User> valueOper = redisTemplate.opsForValue();
- User user = new User("zhangsan",12);
- valueOper.set("user:1", user);
- System.out.println(valueOper.get("user:1").getName());
其中User为pojo类,且需要实现Serializable接口。
二.sdr与json
1) spring配置:
- <bean id="jsonSerializer" class="com.sample.redis.sdr.JsonRedisSerializer"/>
- <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="jedisConnectionFactory"></property>
- <property name="defaultSerializer">
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- </bean>
并没有在配置文件中,使用JacksonJsonRedisSerializer,因为这样实在是麻烦而且不灵活(主要是jackson需要ClassType)。我们将在java代码进行转换,因为通过java代码,使用jackson工具将json字符串转换成javabean是非常简单的。
2) 程序实例:
- /**
- * 不使用sdr自带的json序列化工具,一切操作基于string
- **/
- public class JsonRedisSeriaziler{
- public static final String EMPTY_JSON = "{}";
- public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
- protected ObjectMapper objectMapper = new ObjectMapper();
- public JsonRedisSeriaziler(){}
- /**
- * java-object as json-string
- * @param object
- * @return
- */
- public String seriazileAsString(Object object){
- if (object== null) {
- return EMPTY_JSON;
- }
- try {
- return this.objectMapper.writeValueAsString(object);
- } catch (Exception ex) {
- throw new SerializationException("Could not write JSON: " + ex.getMessage(), ex);
- }
- }
- /**
- * json-string to java-object
- * @param str
- * @return
- */
- public <T> T deserializeAsObject(String str,Class<T> clazz){
- if(str == null || clazz == null){
- return null;
- }
- try{
- return this.objectMapper.readValue(str, clazz);
- }catch (Exception ex) {
- throw new SerializationException("Could not write JSON: " + ex.getMessage(), ex);
- }
- }
- }
- public class RedisClientTest {
- private JsonRedisSeriaziler seriaziler;
- private RedisTemplate redisTemplate;
- public void setSeriaziler(JsonRedisSeriaziler seriaziler) {
- this.seriaziler = seriaziler;
- }
- public void setRedisTemplate(RedisTemplate redisTemplate) {
- this.redisTemplate = redisTemplate;
- }
- public void insertUser(User user){
- ValueOperations<String, String> operations = redisTemplate.opsForValue();
- operations.set("user:" + user.getName(), seriaziler.seriazileAsString(user));
- }
- public User getUser(String name){
- ValueOperations<String, String> operations = redisTemplate.opsForValue();
- String json = operations.get("user:" + name);
- return seriaziler.deserializeAsObject(json, User.class);
- }
- }
三.sdr与xml
实施办法可以参见本文“sdr与json”,同时参考spring-oxm相关文档。