Search code examples
spring-bootcachingredisspring-data-redisspring-cache

Spring cache using redis does not store data in the cache


I have a spring boot 2.4.1 application where I configured spring cache with redis.

I have a service with the following method:

    @Cacheable(value = "myCache", key = "'fixed'")
    public Boolean isAuthorized(AuthorizationRequest request){
        return authorizationFeignClient.isAuthorized(request);
    }

Every time I call the method I see something like this in the logs:

2023.04.07;11:16:30 [reactor-http-epoll-3] [rootId:  parentId:  eventId: ] TRACE o.s.c.interceptor.CacheInterceptor  - Computed cache key 'fixed' for operation Builder[public java.lang.Boolean com.example.AuthorizationClientService.isAuthorized(com.example.model.dto.AuthorizationRequest)] caches=[myCache] | key=''fixed'' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
2023.04.07;11:16:30 [reactor-http-epoll-3] [rootId:  parentId:  eventId: ] TRACE o.s.c.interceptor.CacheInterceptor  - No cache entry for key 'fixed' in cache(s) [myCache]
2023.04.07;11:16:30 [reactor-http-epoll-3] [rootId:  parentId:  eventId: ] TRACE o.s.c.interceptor.CacheInterceptor  - Computed cache key 'fixed' for operation Builder[public java.lang.Boolean com.example.AuthorizationClientService.isAuthorized(com.example.model.dto.AuthorizationRequest)] caches=[myCache] | key=''fixed'' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

And on redis the key is never created.

I've debugged the org.springframework.cache.interceptor.CacheAspectSupport and can see that the the code that stores the data on the cache gets executed without error, but still nothing.

I don't know what else to try. Any idea?


Solution

  • It was a configuration error, I had this on my application.yml

    spring:
      cache:
        type: redis
        cache-names: myCache
        redis:
          host: localhost
          port: 6379
          time-to-live: 3600
    

    And changed it to:

    spring:
      cache:
        type: redis
        cache-names: myCache
      data:
        redis:
          host: localhost
          port: 6379
    

    The weird behavior was that if I stopped redis the application failed so I assumed that the redis connection was correct.

    Edit:

    It looks like the main problem was the time-to-live parameter as it must be defined in miliseconds and not in seconds.