spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是很是便捷。sdr提供了4种内置的serializer:redis
- JdkSerializationRedisSerializer:使用JDK的序列化手段(serializable接口,ObjectInputStrean,ObjectOutputStream),数据以字节流存储
- StringRedisSerializer:字符串编码,数据以string存储
- JacksonJsonRedisSerializer:json格式存储
- OxmSerializer:xml格式存储
其中JdkSerializationRedisSerializer和StringRedisSerializer是最基础的序列化策略,其中“JacksonJsonRedisSerializer”与“OxmSerializer”都是基于stirng存储,所以它们是较为“高级”的序列化(最终仍是使用string解析以及构建java对象)。spring
RedisTemplate中须要声明4种serializer,默认为“JdkSerializationRedisSerializer”:json
1) keySerializer :对于普通K-V操做时,key采起的序列化策略
2) valueSerializer:value采起的序列化策略
3) hashKeySerializer: 在hash数据结构中,hash-key的序列化策略
4) hashValueSerializer:hash-value的序列化策略数据结构
不管如何,建议key/hashKey采用StringRedisSerializer。app
接下来,经过实例描述如何使用它们,能够首先参考“spring-data-redis特性”:工具
一. JdkSerializationRedisSerializer/StringRedisSerializerthis
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>
<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) 程序实例:spa
- 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());
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>
<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);
- }
- }
- }
/** * 不使用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);
- }
- }