为什么在将 Jedis 与 Spring Data 结合使用时,数据会以奇怪的键存储在 Redis 中? [英] Why is data getting stored with weird keys in Redis when using Jedis with Spring Data?

查看:42
本文介绍了为什么在将 Jedis 与 Spring Data 结合使用时,数据会以奇怪的键存储在 Redis 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Spring Data Redis 与 Jedis 一起使用.我正在尝试使用键 vc:${list_id} 存储散列.我能够成功插入到redis.但是,当我使用 redis-cli 检查密钥时,我看不到密钥 vc:501381.相反,我看到的是 xacxedx00x05tx00 vc:501381.

I am using Spring Data Redis with Jedis. I am trying to store a hash with key vc:${list_id}. I was able to successfully insert to redis. However, when I inspect the keys using the redis-cli, I don't see the key vc:501381. Instead I see xacxedx00x05tx00 vc:501381.

为什么会发生这种情况,我该如何改变?

Why is this happening and how do I change this?

推荐答案

好的,google了一会儿,在 http://java.dzone.com/articles/spring-data-redis.

Ok, googled around for a while and found help at http://java.dzone.com/articles/spring-data-redis.

这是因为 Java 序列化而发生的.

It happened because of Java serialization.

redisTemplate 的密钥序列化器需要配置为 StringRedisSerializer 即像这样:

The key serializer for redisTemplate needs to be configured to StringRedisSerializer i.e. like this:

<bean 
    id="jedisConnectionFactory" 
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
    p:host-name="${redis.server}" 
    p:port="${redis.port}" 
    p:use-pool="true"/>

<bean 
    id="stringRedisSerializer" 
    class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

<bean 
    id="redisTemplate" 
    class="org.springframework.data.redis.core.RedisTemplate"
    p:connection-factory-ref="jedisConnectionFactory" 
    p:keySerializer-ref="stringRedisSerializer"
    p:hashKeySerializer-ref="stringRedisSerializer" 
/>

现在redis中的key是vc:501381.

Now the key in redis is vc:501381.

或者像@niconic 说的那样,我们也可以将默认序列化器本身设置为字符串序列化器,如下所示:

Or like @niconic says, we can also set the default serializer itself to the string serializer as follows:

<bean 
    id="redisTemplate" 
    class="org.springframework.data.redis.core.RedisTemplate"
    p:connection-factory-ref="jedisConnectionFactory" 
    p:defaultSerializer-ref="stringRedisSerializer"
/>

这意味着我们所有的键和值都是字符串.但是请注意,这可能并不可取,因为您可能希望您的值不仅仅是字符串.

which means all our keys and values are strings. Notice however that this may not be preferable, since you may want your values to be not just strings.

如果您的值是域对象,那么您可以使用 Jackson 序列化程序并按照 此处 所述配置序列化程序,例如这个:

If your value is a domain object, then you can use Jackson serializer and configure a serializer as mentioned here i.e. like this:

<bean id="userJsonRedisSerializer" class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">
    <constructor-arg type="java.lang.Class" value="com.mycompany.redis.domain.User"/>
</bean>

并将您的模板配置为:

<bean 
    id="redisTemplate" 
    class="org.springframework.data.redis.core.RedisTemplate"
    p:connection-factory-ref="jedisConnectionFactory" 
    p:keySerializer-ref="stringRedisSerializer"
    p:hashKeySerializer-ref="stringRedisSerializer" 
    p:valueSerialier-ref="userJsonRedisSerializer"
/>

这篇关于为什么在将 Jedis 与 Spring Data 结合使用时,数据会以奇怪的键存储在 Redis 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆