spring-projects/spring-boot

Consider defining each cache configuration separately

cxjava opened this issue · 1 comments

Currently I defind the cache configuration in our project like below:

cache:
    cache-names:
        CACHE_TEMPLATE: maximumSize=100,expireAfterWrite=10m
        CACHE_MANAGER: maximumSize=50,expireAfterWrite=1d
        TOKEN_CACHE: maximumSize=20,expireAfterWrite=29m
        OS_CACHE: maximumSize=5,expireAfterWrite=59m,refreshAfterWrite=20m
@Configuration
@ConfigurationProperties(prefix = "cache")
@EnableCaching
public class CacheConfig {
    private Map<String, String> cacheNames;

    @Bean
    public CacheManager caffeineCacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        ArrayList<CaffeineCache> caches = new ArrayList<CaffeineCache>();
        for (Map.Entry<String, String> cacheDef : cacheNames.entrySet()) {
            caches.add(new CaffeineCache(cacheDef.getKey(),
                    Caffeine.newBuilder()
                            .from(cacheDef.getValue())
                            .build())
            );
        }

        cacheManager.setCaches(caches);
        return cacheManager;
    }
}

Is it possible to add this functionality in later versions ? It may be like this:

spring:
    cache:
        type: caffeine
        cache-names:
            CACHE_TEMPLATE: maximumSize=100,expireAfterWrite=10m
            CACHE_MANAGER: maximumSize=50,expireAfterWrite=1d
            TOKEN_CACHE: maximumSize=20,expireAfterWrite=29m
            OS_CACHE: maximumSize=5,expireAfterWrite=59m,refreshAfterWrite=20m

Thanks for the suggestion but I am not keen to bring that configuration complexity at this point. This was already requested and declined btw

Duplicates #7235