嗨下面是我的redis缓存配置,我正在尝试使用以下方法获取redis服务器上存储的所有缓存名称:
rediscachemanager.getcachenames()
但是它没有给我redis服务器中的cachename。
我使用cachable注释作为cachename前缀:@cacheable(“cachename”)
我有两个问题?
1)为什么我无法获取我存储在redis服务器中的缓存名。
2)由于我不想为所有前缀cachename都编写单独的方法,所以如何动态创建前缀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;
}
最佳答案
对于缓存名,请尝试
@Autowired CacheManager cacheManager
cacheManager.getCacheNames().forEach(cacheM-> {System.out.println(cacheM);});