0

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) {
} 
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

2 Answers2

1

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

No because cacheNames are (work like) namespaces so you can use same keys in different namespaces.

akuma8
  • 4,160
  • 5
  • 46
  • 82
1

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}::.

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184