gpt4 book ai didi

java - Spring Redis 问题 : GetAllCacheNames from redis cache is not working with RedisCacheManager

转载 作者:可可西里 更新时间:2023-11-01 11:25:23 29 4
gpt4 key购买 nike

您好,下面是我的 redis 缓存配置,我正在尝试使用以下方式获取存储在 redis 服务器上的所有缓存名称:

redisCacheManager.getCacheNames()

但它没有给我 redis 服务器中存在的缓存名称。

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

我在这里有 2 个问题?

1) 为什么我无法获取存储在 redis 服务器中的缓存名称。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;
}

最佳答案

对于CacheNames,尝试

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

关于java - Spring Redis 问题 : GetAllCacheNames from redis cache is not working with RedisCacheManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46056087/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com