Spring-data-redis ping 有效,密钥存在,没有返回数据

Jef*_*ord 2 spring redis spring-data

这是我使用 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" />
Run Code Online (Sandbox Code Playgroud)

这是我的 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;
}
Run Code Online (Sandbox Code Playgroud)

以下是日志文件的相关输出:

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
Run Code Online (Sandbox Code Playgroud)

请注意从 Redis 返回的 TestVal 是如何为null 的,但是如果我针对该服务器的 0(零)数据库使用 redis-cli,我会得到以下响应:

127.0.0.1:6379> get akey
"yo mama"
Run Code Online (Sandbox Code Playgroud)

其中“哟妈妈”是我期待返回的值。


@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;
}
Run Code Online (Sandbox Code Playgroud)

注意:添加StringRedisSerializer作为 RedisTemplate 的默认序列化器。感谢您的努力。

mp9*_*1de 5

RedisOperations使用序列化器Java 对象转换为 Redis 数据结构值。序列化器默认为JdkSerializationRedisSerializer. JDK 序列化程序将String对象转换为与 ASCII 或 UTF-8 不兼容的 Java 序列化表示形式。查看文档,您可能会对StringRedisSerializer. 序列化器需要设置为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;
}
Run Code Online (Sandbox Code Playgroud)