2

This is my first application using spring-data-redis and I think I get the concepts pretty well (I've used JdbcTemplate many times with RDBMS-es in the past). Here's what's happening...

I've set up a RedisTemplate using a JedisConnectionFactory and am able to successfully ping the Redis server. I'm not able to get the simplest data response from the server however and I'm afraid I'm missing something fundamental that I've not been able to divine from documentation so far.

Here's the Redis section of my bean.xml file:

<!-- Redis DAO stuff -->
<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="${redis.url}" p:port="${redis.port}" p:database="0" />

and here is the relevant section of code in my RedisDAO class:

@Autowired
private RedisTemplate<String, Object> template;

public String getTestVal() {
    logger.debug("getTestVal() function called++++++++++++++++++++");
    template.getConnectionFactory().getConnection().select(0);
    logger.debug("22222222222222");
    String pingResult = template.getConnectionFactory().getConnection().ping();
    logger.debug("333333333333333");
    logger.debug("REDIS PING RESULT: " + pingResult);
    logger.debug("444444444444444");
    logger.debug("HasKey Result: " + template.hasKey("akey"));
    logger.debug("555555555555555");
    //NAC;P3_TZ_380002878
    Object testVal = template.opsForValue().get("akey");
    logger.debug("TestVal returned from REdis: " + testVal);
    return null;
}

Here is the relevant output of the log file:

13:48:21.505 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - getTestVal() function called++++++++++++++++++++
13:48:21.678 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - 22222222222222
13:48:21.801 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - 333333333333333
13:48:21.801 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - REDIS PING RESULT: PONG
13:48:21.801 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - 444444444444444
13:48:21.808 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:48:21.936 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:48:21.937 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - HasKey Result: false
13:48:21.937 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - 555555555555555
13:48:21.940 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
13:48:22.009 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection
13:48:22.009 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - TestVal returned from REdis: null

Note how the TestVal returned from Redis is null, but if I use redis-cli against the 0(zero) database of that server, I get the following response:

127.0.0.1:6379> get akey
"yo mama"

Where "yo mama" is the value I'm expecting back.


@mp911de I'm responding here because the comment response wasn't long enough to show detail. I'm afraid that I'm still having the same issue when I change the code to the following (and no other changes)...
public String getTestVal() {
    template.setDefaultSerializer(new StringRedisSerializer());
    template.afterPropertiesSet();
    logger.debug("getTestVal() function called++++++++++++++++++++");
    template.getConnectionFactory().getConnection().select(0);
    logger.debug("22222222222222");
    String pingResult =  template.getConnectionFactory().getConnection().ping();
    logger.debug("333333333333333");
    logger.debug("REDIS PING RESULT: " + pingResult);
    logger.debug("444444444444444");
    logger.debug("HasKey Result: " + template.hasKey("akey"));
    logger.debug("555555555555555");
    Object testVal = template.opsForValue().get("akey");
    logger.debug("TestVal returned from REdis: " + testVal);

    return null;
}

Note: The addition of the StringRedisSerializer as the RedisTemplate's default serializer. Thanks for the effort.

Jeff Orford
  • 93
  • 3
  • 10

2 Answers2

3

RedisOperations uses serializers to translate Java objects into Redis data structure values. The serializer defaults to JdkSerializationRedisSerializer. The JDK serializer translates your String object into a Java-serialized representation that is not compatible with ASCII or UTF-8. Check out the docs, you might be interested in StringRedisSerializer. The serializer needs to be set in RedisTemplate:

@Bean
RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();

    redisTemplate.setDefaultSerializer(new StringRedisSerializer());

    redisTemplate.setConnectionFactory(redisConnectionFactory);
    redisTemplate.afterPropertiesSet();
    return redisTemplate;
}
mp911de
  • 14,871
  • 2
  • 41
  • 82
  • In addition to @mp911de 's help, I found this [question/answer](http://stackoverflow.com/questions/13215024/weird-redis-key-with-spring-data-jedis) which was also helpful. The key change seemed to be setting the serializer in the xml bean config file. Worked like a charm after that. – Jeff Orford Mar 04 '16 at 17:56
0

Yes, you need a serializer for redisTemplate to return data. With Jedis, you don't need it.

@Bean(name = "redisTemplate")
public RedisTemplate<String, String> redisTemplate() {
    RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
    redisTemplate.setConnectionFactory(redisConnectionFactory());
    redisTemplate.setDefaultSerializer(new StringRedisSerializer());
    return redisTemplate;
}
QBrute
  • 3,470
  • 6
  • 30
  • 36
user5796415
  • 31
  • 1
  • 3