Skip to content
Advertisement

Get Set value from Redis using RedisTemplate

I am able to retrieve values from Redis using Jedis:

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();
    }

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?

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" />

Advertisement

Answer

In short

You have to configure serializers.

Explanation

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

"xacxedx00x05tx00x03key"

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

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

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

and then in the redis-cli

127.0.0.1:6379> keys *
1) "xacxedx00x05tx00x05myKey"
127.0.0.1:6379> SMEMBERS "xacxedx00x05tx00x05myKey"
1) "xacxedx00x05srx00x0ejava.util.Datehjx81x01KYtx19x03x00x00xpwbx00x00x01Nxcf#x9cHx"

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

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

Java configuration

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

XML configuration

<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 has some interesting serializers that allow message exchange between various systems. You can choose either from the built-in serializers

  • JacksonJsonRedisSerializer
  • Jackson2JsonRedisSerializer
  • JdkSerializationRedisSerializer (default)
  • OxmSerializer
  • GenericToStringSerializer

or create your own.

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

Further read:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement