【发布时间】:2022-01-17 13:09:59
【问题描述】:
我在 Spring Boot 服务中使用 RedisTemplate 进行缓存。现在我想通过端点执行器/prometheus 检查缓存命中/缓存未命中。但看不到缓存的缓存命中/缓存未命中。 我写的代码如下所示
@EnableCaching
@Configuration
public class CachingConfiguration {
@Bean
public RedisTemplate<String, SomeData> redisTemplate(LettuceConnectionFactory connectionFactory, ObjectMapper objectMapper)
{
RedisTemplate<String, SomeData> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
var valueSerializer = new Jackson2JsonRedisSerializer<SomeData>(SomeData.class);
valueSerializer.setObjectMapper(objectMapper);
template.setValueSerializer(valueSerializer);
return template;
}
}
现在正在执行如下操作来获取并保存到缓存中 得到:-
redisTemplate.opsForValue().get(key);
然后保存:-
redisTemplate.opsForValue().set(key, obj, some_time_limit);
我的缓存工作正常,能够保存到缓存中并获得正确的数据。 但我没有在执行器/prometheus 中看到缓存命中/未命中相关数据。 在我的 application.yml 文件中,我在下面添加了
cache:
redis:
enable-statistics: 'true'
【问题讨论】:
标签: spring-boot redis prometheus spring-boot-actuator spring-data-redis