Redis 使用spring-data-redis的序列化问题
2016-12-01 10:59
603 查看
我用spring-data-redis 成功的set了 而且也能成功get出来对应的数据~
但是用redis-cli去服务器直接get数据是提示(nil) 这是什么原因啊?
正常是get一个不存在的key才会这样的
突然想起spring-data-redis 对 key 和 value 都进行了序列化 变成byte[] 再调用对应的redis java client进行存储的。 那应该就是通过spring-data-redis进入redis的key变了
原因其实就出现在这里,解决的办法就是手动定义序列化的方法,spring-data-redis中还提供了一个序列化的类专门针对string类型的序列化org.springframework.data.redis.serializer.StringRedisSerializer这个类,可以在xml里面指定:
如果没有特殊的设置,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 进行序列化
但是用redis-cli去服务器直接get数据是提示(nil) 这是什么原因啊?
正常是get一个不存在的key才会这样的
突然想起spring-data-redis 对 key 和 value 都进行了序列化 变成byte[] 再调用对应的redis java client进行存储的。 那应该就是通过spring-data-redis进入redis的key变了
原因其实就出现在这里,解决的办法就是手动定义序列化的方法,spring-data-redis中还提供了一个序列化的类专门针对string类型的序列化org.springframework.data.redis.serializer.StringRedisSerializer这个类,可以在xml里面指定:
<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" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </bean>
如果没有特殊的设置,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 进行序列化
相关文章推荐
- spring-data-redis使用自定义序列化数据 使用 protobuf
- 【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation
- spring data redis serializer SerializationException 序列化问题
- jedis,spring-redis-data 整合使用,版本问题异常
- 使用Spring Data Redis实现缓存遇到的一些问题
- jedis,spring-redis-data 整合使用,版本问题异常以及解决。
- 使用Spring Data Redis时,遇到的几个问题
- spring-data-redis 整合,以及使用kryo序列化代替jdk原生序列化机制
- spring-data-redis使用jdk序列化时increment的异常
- 使用Spring Data Redis时,遇到的几个问题
- 使用Spring Data Redis时,遇到的几个问题
- springboot中使用redis,key序列化问题解决方案
- spring boot data RedisTemplate 序列化问题
- spring-data-redis 使用过程中需要注意的一点(序列化选择)
- 使用Spring Data Redis时,遇到的几个问题
- spring-data-redis 的序列化问题
- spring-data-redis 使用 protobuf进行序列化和反序列
- spring-data-redis 使用
- 使用spring-data-redis操作Redis的Sentinel
- 关于Spring Data redis几种对象序列化的比较