使用RedisTemplate从Redis获取设置值 [英] Get Set value from Redis using RedisTemplate

查看:445
本文介绍了使用RedisTemplate从Redis获取设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用JedisRedis检索值:

public static void main(String[] args) {
        Jedis jedis = new Jedis(HOST, PORT);
        jedis.connect();
        Set<String> set = jedis.smembers(KEY);
        for (String s : set) {
            System.out.println(s);
        }
        jedis.disconnect();
        jedis.close();
    }

但是当我尝试使用Spring的RedisTemplate时,我没有得到任何数据.我的数据以Set的形式存储在Redis中.

But when I am trying to use Spring's RedisTemplate , I am not getting any data. My data is stored in Redis as a Set.

      // inject the actual template 
      @Autowired
      private RedisTemplate<String, Object> template;

      // inject the template as SetOperations
      @Resource(name="redisTemplate")
      private SetOperations<String,String> setOps;

public String logHome() {       
        Set<String> set =  setOps.members(KEY);
        for(String str:set){
            System.out.println(str); //EMPTY
        }       
        Set<byte[]> keys = template.getConnectionFactory().getConnection().keys("*".getBytes());
        Iterator<byte[]> it = keys.iterator();
        while(it.hasNext()){
            byte[] data = (byte[])it.next();
            System.out.println(new String(data, 0, data.length)); //KEYS are printed.
        }
        Set<Object> mySet = template.boundSetOps(KEY).members();        
        System.out.println(mySet); //EMPTY      
        return "";
    }

有人可以向我指出我在想什么吗?

Can someone please point out to me what am I missing?

我的RedisTemplate的xml配置.

EDIT : My xml config for RedisTemplate.

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

     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:host-name="myhostname" p:port="6379" />

推荐答案

简而言之

您必须配置序列化器.

In short

You have to configure serializers.

Redis模板将序列化程序用于键,值和哈希键/值.序列化器用于将Java输入转换为Redis中存储的表示形式.如果不进行任何配置,则序列化程序默认为JdkSerializationRedisSerializer.因此,如果您在Java代码中要求输入密钥key,则序列化程序会将其转换为

The Redis template uses serializers for keys, values and hash keys/values. Serializers are used to convert the Java input into the representation that is stored within Redis. If you do not configure anything, the serializer defaults to JdkSerializationRedisSerializer. So if you ask for a key key in your Java code, the serializer converts it to

"\xac\xed\x00\x05t\x00\x03key"

并且Spring Data Redis使用这些字节作为查询Redis的键.

and Spring Data Redis uses those bytes as the key to query Redis.

您可以使用Spring Data Redis添加数据并使用redis-cli进行查询:

You can add data with Spring Data Redis and query it using the redis-cli:

template.boundSetOps("myKey").add(new Date());

,然后在redis-cli

127.0.0.1:6379> keys *
1) "\xac\xed\x00\x05t\x00\x05myKey"
127.0.0.1:6379> SMEMBERS "\xac\xed\x00\x05t\x00\x05myKey"
1) "\xac\xed\x00\x05sr\x00\x0ejava.util.Datehj\x81\x01KYt\x19\x03\x00\x00xpw\b\x00\x00\x01N\xcf#\x9cHx"

如您所见,字符串"和日期"被序列化为代表Java序列化对象的一些疯狂字节.

As you see, the String and the Date are serialized into some crazy bytes that represent a Java-serialized object.

您的代码建议您要存储基于字符串的键和值.只需在您的RedisTemplate

Your code suggests you want to store String-based keys and values. Just set the StringRedisSerializer in your RedisTemplate

Java配置

redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());

XML配置

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

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

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
    p:host-name="myhostname" p:port="6379"/>

运行代码后的输出如下:

The output after running your code looks like then:

value
key
[value]

Spring Data Redis具有一些有趣的序列化器,这些序列化器允许在各种系统之间交换消息.您可以从内置的序列化器中选择

Spring Data Redis has some interesting serializers that allow message exchange between various systems. You can choose either from the built-in serializers

  • JacksonJsonRedisSerializer
  • Jackson2JsonRedisSerializer
  • JdkSerializationRedisSerializer(默认)
  • OxmSerializer
  • GenericToStringSerializer

或创建自己的.

我使用Spring Data Redis 1.5.1.RELEASE和jedis 2.6.2来验证问题的结果. HTH,马克

I used Spring Data Redis 1.5.1.RELEASE and jedis 2.6.2 to verify the result of your question. HTH, Mark

进一步阅读:

  • Spring Data Redis: Serializers
  • Gist containing your example

这篇关于使用RedisTemplate从Redis获取设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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