【问题标题】:Spring jedis get key list return emptySpring jedis 获取密钥列表返回空
【发布时间】:2019-02-13 22:17:45
【问题描述】:

我正在尝试从 redis 获取密钥列表,但它不起作用。

    @Autowired
    org.springframework.data.redis.core.RedisTemplate redisTemplate;

    redisTemplate.opsForValue().set("test","test");
    redisTemplate.opsForValue().set("t:test","test");
    redisTemplate.opsForValue().set("t::test1","test");
    redisTemplate.opsForValue().set("t1.t2::test2","test");
    Set<String> keys = redisTemplate.keys("t*");

我为键“*”、“t:*”、“t::*”尝试了不同的模式。没有任何效果。 只有我写了完整的键名才有效。

创建 bean 代码:

@Bean
RedisTemplate<String,Object> redisTemplate(@Autowired JedisConnectionFactory jedisConnectionFactory){
    RedisTemplate<String,Object> template=new RedisTemplate<>();
    template.setConnectionFactory(jedisConnectionFactory);
    return template;
}

@Bean
JedisConnectionFactory jedisConnectionFactory(@Value("${redis.host:192.168.99.100}") String host, @Value("${redis.port:6379}") int port, @Value("${redis.password:}") String password){
    RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port);
    redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
    return new JedisConnectionFactory(redisStandaloneConfiguration);
}

【问题讨论】:

  • 你应该已经在你的bean redisTemplate.setDefaultSerializer(new StringRedisSerializer())中配置了redisTemplate和StringRedisSerializer;
  • 在另一个 SO 帖子下讨论了类似问题的许多答案。参考stackoverflow.com/questions/19098079/…
  • 感谢@GovindParasha。通过添加密钥序列化程序解决了问题。 template.setKeySerializer(new StringRedisSerializer());

标签: java spring redis jedis


【解决方案1】:

添加密钥序列化程序。

template.setKeySerializer(new StringRedisSerializer());

【讨论】:

    猜你喜欢
    • 2017-11-11
    • 2016-02-03
    • 1970-01-01
    • 2019-11-04
    • 2012-04-16
    • 1970-01-01
    • 2011-09-25
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多