这是我第一次使用spring-data-redis的应用程序,我想我得到的概念非常好(我已经多次使用JdbcTemplate与RDBMS-es以往)。以下是发生了什么...Spring-data-redis ping工作,键是那里,没有数据返回
我已经使用JedisConnectionFactory设置了一个RedisTemplate,并且能够成功地ping通Redis服务器。我无法从服务器获得最简单的数据响应,但我担心我错过了迄今为止从文档中无法获得的基本信息。
这是我的bean.xml文件的Redis的部分:
<!-- 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" />
这里是代码的相关章节中我RedisDAO类:
@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;
}
这里是日志的相关输出文件:
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
请注意如何从Redis返回的TestVal是null,但如果我使用Redis的-CLI针对服务器的0(零)数据库中,我得到如下回应:
127.0.0.1:6379> get akey
"yo mama"
其中“哟妈妈”是我期待后面的值。
@ mp911de我在这里回应,因为评论的回应不够长,不足以显示细节。 恐怕我仍然有同样的问题,当我的代码更改为以下(且并无其他变动)...
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;
}
注:的StringRedisSerializer的加入为RedisTemplate的默认序列化程序。感谢您的努力。
除了@ mp911de的帮助下,我发现这个[提问/回答(http://stackoverflow.com/questions/13215024/weird-redis-key-with-spring-data-杰迪斯)这也是有帮助的。关键的变化似乎是在xml bean配置文件中设置序列化程序。之后就像魅力一样工作。 –