论坛首页 Java企业应用论坛

spring-data-redis 的序列化问题

浏览 9042 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-08-09  

前几天有人在我的博文中留言,询问spring-data-redis的问题,如下,黑体为我的回答

 

 

我用spring-data-redis 成功的set了 而且也能成功get出来对应的数据~

但是用redis-cli去服务器直接get数据是提示(nil) 这是什么原因啊?

 

正常是get一个不存在的key才会这样的

 

但是我用的是同一个key啊···通过spring-data-redis就可以成功get出来~~

如果我直接用jedis存的话就没有这样的问题·· 大侠解惑啊····

 


我在项目中都是直接用jedis

突然想起spring-data-redis 对 key 和 value 都进行了序列化 变成byte[] 再调用对应的redis java client进行存储的。  那应该就是通过spring-data-redis进入redis的key变了

 

 

今天就这个问题详细分析一下

 

可能我们正常使用redis时 key 和 value 一般都是 string 类型的

但client 和 server 间的协议要求的是 byte   可参看我的另一篇博文 http://jimgreat.iteye.com/blog/1586671

Jedis 提供了 string 和 byte[] 类型的函数接口

 

 

spring-data-redis  中的核心操作类是  RedisTemplate<K, V>

可以看出 key 和 value 都是泛型的,这就涉及到将类型进行序列化的问题了

所就在 RedisTemplate 中还有几个 RedisSerializer

 

 

	private RedisSerializer<?> defaultSerializer = new JdkSerializationRedisSerializer();

	private RedisSerializer keySerializer = null;
	private RedisSerializer valueSerializer = null;
	private RedisSerializer hashKeySerializer = null;
	private RedisSerializer hashValueSerializer = null;
	private RedisSerializer<String> stringSerializer = new StringRedisSerializer();
 

如果没有特殊的设置,key 和 value 都是使用 defaultSerializer = new JdkSerializationRedisSerializer(); 进行序列化的。

 

对于 key = "AAAA"  value = "cccc" 的情况, server 端运行的情况如下

 

 "SET" "\xac\xed\x00\x05t\x00\x04AAAA" "\xac\xed\x00\x05t\x00\x04cccc"

 "GET" "\xac\xed\x00\x05t\x00\x04AAAA"

 

如果项目中只使用了string的 key 和 value ,显然这样不适合在sever上进行debug

 

通过下面的配置,可以改成使用StringRedisSerializer对 key 和 value 进行序列化

 

 

  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
    p:connection-factory-ref="jedisConnectionFactory" >
       <property name="KeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>
        <property name="ValueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>       
    </bean>

 

这样就能在 server 上找到你原来的 key 了 

 

 "SET" "AAAA" "cccc"

 "GET" "AAAA"

 

对于Hash结构内部的 key 和 value 可以添加如下配置

 

 

        <property name="HashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property>  
        <property name="HashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
        </property> 
   发表时间:2012-08-09  
吊丝才用 spring-data-redis
0 请登录后投票
   发表时间:2012-08-28  
我现在想要存入key-value : String-Json 这样注入:

<property name="KeySerializer"> 
          <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean> 
      </property> 
      <property name="ValueSerializer"> 
          <bean class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer"></bean> 
      </property> 
但是JacksonJsonRedisSerializer 要需要泛型··  这样当怎么注入啊?
0 请登录后投票
   发表时间:2012-08-28  
javasoftlover 写道
我现在想要存入key-value : String-Json 这样注入:

<property name="KeySerializer"> 
          <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean> 
      </property> 
      <property name="ValueSerializer"> 
          <bean class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer"></bean> 
      </property> 
但是JacksonJsonRedisSerializer 要需要泛型··  这样当怎么注入啊?



我试了一下 下面是可以的

        User u = new User();
        u.setId(1);
        u.setName("jimgreat");       

        RedisTemplate<String, User> template = (RedisTemplate<String, User>)context.getBean("redisTemplate");
        JacksonJsonRedisSerializer<User> json = new JacksonJsonRedisSerializer<User>(User.class);       
        template.setValueSerializer(json);
       
       
        template.opsForValue().set("user:1",u); 
        System.out.println(template.opsForValue().get("user:1"));


JacksonJsonRedisSerializer有一个构造函数  下面这个
public JacksonJsonRedisSerializer(Class<T> type) {
this.javaType = TypeFactory.type(type);
}
应该是在注入时没法指定type
0 请登录后投票
   发表时间:2012-08-28  
我刚也试了一下·· 但是在redis-cli 这个客户端下· 还是不能正确先是结果···
hduser@appdoServer2:~$ redis-cli get BASE-1-APP-BASE-1-9
ZautoUpdateStatusZ.core.app.pojo.Application
pushMsgSwitchZwidgetAdStatusZtchZ
                             widgetSwitchLanReportMethodt5Lorg/zywx/appdo/core/Constants$AnalyticsReportMethod;LappKeytLjava/lang/String;L createdAttLjava/util/Date;Lcreatorq~Lidq~Lnameq~xppwangtian.miaoBASE-1-9t969-8cf2c4131c28pt
                      testsafdasfd
0 请登录后投票
   发表时间:2012-08-28  
javasoftlover 写道
我刚也试了一下·· 但是在redis-cli 这个客户端下· 还是不能正确先是结果···
hduser@appdoServer2:~$ redis-cli get BASE-1-APP-BASE-1-9
ZautoUpdateStatusZ.core.app.pojo.Application
pushMsgSwitchZwidgetAdStatusZtchZ
                             widgetSwitchLanReportMethodt5Lorg/zywx/appdo/core/Constants$AnalyticsReportMethod;LappKeytLjava/lang/String;L createdAttLjava/util/Date;Lcreatorq~Lidq~Lnameq~xppwangtian.miaoBASE-1-9t969-8cf2c4131c28pt
                      testsafdasfd


在redis-cli下 用monitor命令看一下 写入的东西是什么
http://redis.io/commands/monitor
0 请登录后投票
   发表时间:2012-08-28  
汗·· 我这没有那个命令 可能是版本太低了?·· 这是公司的服务器 我的权限也有限···
hduser@appdoServer2:~$ redis-cli monitor
Unknown command 'monitor'
hduser@appdoServer2:~$
0 请登录后投票
   发表时间:2012-08-28  
算了·· 我还是用JSONObject 把他们都转换成String 然后用String存储吧····
get的时候在用JSONObject转化成对应的bean··· 这样改起来也不麻烦···
谢谢了··这东西弄的太久了·· 公司都有点皮了·· 哈哈··
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics