Search code examples
springspring-data-redis

Does @Cacheable#cacheNames have nothing to do with Redis?


I annotated my service as follows

@Cacheable(cacheNames = {"some"},
           key = "{#req.some, #req.other, #req.property}")
public List<Some> listSome(SomeReq req) {
    ..
}

It seems work and I saw these keys via Medis. (coupon is the value of spring.cache.redis.key-prefix.)

coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}

Where was the cacheNames in action?

Is it possible to collide if I add other service method with same pattern of keys?

@Cacheable(cacheNames = {"someOther"},
           key = "{#req.some, #req.other, #req.property}")    
List<SomeOther> listSomeOthers(SomeOtherReq req) {
} 

Solution

  • I'm answering for my own question for further reachers.

    Simply, I need to defined a bean for a RedisCacheManager to make the cacheNames works.

    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        final RedisCacheManager cacheManager = RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(defaultCacheConfig())
                .build();
        return cacheManager;
    }
    

    As you can see I didn't change anything from defaultCacheConfig().

    And the keys are property prefixed with {cacheName}::.