stepio / coffee-boots

Support property-based configuring of multiple Caffeine caches for Spring Cache abstraction.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add CacheCondition to CaffeineSpecAutoConfiguration to avoid creating bean if not needed

marceloverdijk opened this issue · comments

Add @Conditional({ CacheCondition.class }) to CaffeineSpecAutoConfiguration, similar as Spring Boot's CaffeineCacheConfiguration has this as well.

In this case the coffee-boots auto-configuration will not kick in if spring.cache.type property is not set to caffeine. This is useful with different cache implementation per environment.

Hello @marceloverdijk ,

Once again, thanks for reaching me out and special thanks for using proper channel - I appreciate your suggestions and professionalism.

I initially assumed that if someone adds this project to dependencies, he/she understands the consequences and there is no need to implement extra disabling.

But I missed your case:

different cache implementation per environment

So yeah, that's definitely another good idea to implement. However, I will need to double-check this deeper. For some reason I missed this initially, although I used Spring's configuration as an example. And I missed this again few weeks ago, when I had a really hard time trying to make micrometer metrics work out of the box.

But again - contribution is always welcome. Or I'll try this myself shortly.

Cheers.

Small update: it's impossible to add @Conditional({ CacheCondition.class }) as is, because class CacheCondition is package-private.
Spring team likes hiding non-public API, which is understandable, but adds complications.

Found even better solution:

@ConditionalOnProperty(
        name = "spring.cache.type",
        havingValue = "caffeine",
        matchIfMissing = true
)

Also aligned metrics registration mechanism with this.

Merged