Spring Redis问题:来自redis缓存的GetAllCacheNames不能与RedisCacheManager一起使用

时间:2017-09-05 13:22:05

标签: java redis spring-data spring-data-redis

下面是我的redis缓存配置,我正在尝试使用以下命令将所有cachename存储在redis服务器上:

redisCacheManager.getCacheNames()

但它没有给我redis服务器中的cacheName。

我使用Cachable注释为cacheName添加前缀:@Cacheable(“cacheName”)

我这里有2个问题?

1)为什么我无法获取存储在redis服务器中的cacheNames。 2)如何动态创建前缀cacheName,因为我不想为所有这些创建单独的方法?

@Bean
JedisConnectionFactory jedisConnectionFactory() {
    JedisConnectionFactory factory = new JedisConnectionFactory();
    factory.setHostName(redisHost);
    factory.setPort(redisPort);
    factory.setUsePool(true);
    return factory;
}

@Bean
RedisTemplate<Object, Object> redisTemplate() {
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    redisTemplate.setDefaultSerializer(new StringRedisSerializer());
   // redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
    return redisTemplate;
}

@Bean
CacheManager cacheManager() {
    // configuration to set the time to expire in seconds, messageCache is name of method
    Map<String,Long> cacheMap = new HashMap<String, Long>();
    //cacheMap is the name of the map where we put all the method names for which we want time to expire.
    RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
    redisCacheManager.setUsePrefix(true);
    //redisCacheManager.setExpires(cacheMap);
    return redisCacheManager;
}

1 个答案:

答案 0 :(得分:0)

对于CacheNames,请尝试

@Autowired CacheManager cacheManager     
cacheManager.getCacheNames().forEach(cacheM-> {System.out.println(cacheM);});