RedisTemplate与RedisConnection相比未获取相同的密钥

时间:2018-09-28 10:44:11

标签: spring redis

我正在尝试获取本地Redis服务器的所有密钥。我已经这样配置RedisTemplate:

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
    configuration.setDatabase(0);
    configuration.setHostName("localhost");
    configuration.setPort(12000);
    return new JedisConnectionFactory(configuration);
}

@Bean
public RedisTemplate<String, Object> redisTemplate() {
    final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
    template.setConnectionFactory(jedisConnectionFactory());
    template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class));
    return template;
}

当我使用RedisConnection在Redis服务器中检索密钥时,它会正确获取所有密钥:

RedisConnection redisConnection = template.getConnectionFactory().getConnection();
Set<byte[]> keys = redisConnection.keys("*".getBytes());
for (byte[] string : keys) {
 System.out.println(new String(string));
}
redisConnection.close();

但是当我尝试使用RedisTemplante进行相同的操作时,它根本无法获取任何内容:

Set<String> keys = template.keys("*");
for (String string : keys) {
 System.out.println(string);
}

我缺少什么?

预先感谢

0 个答案:

没有答案