【发布时间】:2016-06-17 10:02:08
【问题描述】:
这是我第一个使用 spring-data-redis 的应用程序,我认为我很好地理解了这些概念(过去我曾多次使用 JdbcTemplate 和 RDBMS-es)。这就是正在发生的事情......
我已经使用 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,但是如果我对该服务器的 0(零)数据库使用 redis-cli,我会得到以下响应:
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 的默认序列化器。感谢您的努力。
【问题讨论】:
标签: spring redis spring-data