skip to Main Content

I am using RedisTemplate for caching purpose in my spring boot service. Now I want to check cache hit/cache miss through end point actuator/prometheus. But can not see cache hit/cache miss for the cache.
The code I have written is something like below

    @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;
        }
    }

Now am doing like below to get and save into cache
to get:-

redisTemplate.opsForValue().get(key);

And to save:-

redisTemplate.opsForValue().set(key, obj, some_time_limit);

My cache is working properly, am getting able to save into cache and getting proper data.
But I don’t see cache hit/miss related data inside actuator/prometheus.
In my application.yml file I have added below

cache:
    redis:
      enable-statistics: 'true'

2

Answers


  1. I would assume that in order for Springboot Cache Monitoring to apply (Including Hits/Misses), you would need to depend on AutoConfiguration.

    In your case you are creating the RedisTemplate yourself, and probably enable-statistics is not actually applied.

    Can you remove the redistemplate creation and use @Cacheable annotation abstraction? That way any supported Cache library will work out of the box, without you having to create @Bean and manually configuring it.

    Otherwise, generally if you wanted to enable statistics on a cache manager manually, you will need to call RedisCacheManager.RedisCacheManagerBuilder enableStatistics():

    https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/cache/RedisCacheManager.RedisCacheManagerBuilder.html

    For Reference:

    Auto-configuration enables the instrumentation of all available Cache
    instances on startup, with metrics prefixed with cache. Cache
    instrumentation is standardized for a basic set of metrics.
    Additional, cache-specific metrics are also available.

    Metrics are tagged by the name of the cache and by the name of the
    CacheManager, which is derived from the bean name.

    Only caches that are configured on startup are bound to the registry. For caches not
    defined in the cache’s configuration, such as caches created on the
    fly or programmatically after the startup phase, an explicit
    registration is required. A CacheMetricsRegistrar bean is made
    available to make that process easier.

    Login or Signup to reply.
  2. I had exactly the same question and spent a good number of hours trying to figure out how to enable cache metrics for my manually created RedisTemplate instance.
    What I eventually realised is that it’s only RedisCache class which collects and exposes CacheStatistics through getStatistics() method. As far as I can see there is nothing like that for RedisTemplate, which means you either need to switch to using RedisCache through RedisCacheManager and @Cacheable annotation or implement your custom metrics collection.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search