See https://github.com/spring-projects/spring-boot/issues/15960#issuecomment-576204233
Comment From: snicoll
Closing in favour of PR #19819
Comment From: uqix
FYI, here is my cache config code:
I want to point out:
For convenience, RedisCacheManagerBuilder could expose defaultCacheConfiguration field since it is immutable
CacheConfig.java
package app.cache;
import java.util.Arrays;
import java.util.HashSet;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager.RedisCacheManagerBuilder;
import org.springframework.stereotype.Component;
import lombok.RequiredArgsConstructor;
@Configuration
@EnableCaching
class CacheConfig {
}
@Component
@RequiredArgsConstructor
class RedisCacheInit implements RedisCacheManagerBuilderCustomizer {
private final CacheProperties cacheProperties;
@Override
public void customize(RedisCacheManagerBuilder builder) {
// get the RedisCacheManagerBuilder.defaultCacheConfiguration which is built well by spring boot
// or using RedisCacheConfiguration.defaultCacheConfig() without classLoader still causes ClassCastException
// maybe for convenience,
// RedisCacheManagerBuilder could expose defaultCacheConfiguration field since it is immutable
var defaultName = "default";
builder.initialCacheNames(new HashSet<>(Arrays.asList(defaultName)));
var defaultConfig = builder.getCacheConfigurationFor(defaultName).get();
cacheProperties.getConfigByName().forEach((name, options) -> {
var config = defaultConfig.entryTtl(options.getTimeToLive());
builder.withCacheConfiguration(name, config);
});
}
}
CacheProperties.java
package app.cache;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;
@Component
@ConfigurationProperties(prefix = "app.cache")
@Data
class CacheProperties {
private Map<String, CacheOptions> configByName = new HashMap<>();
}
@Data
class CacheOptions {
private Duration timeToLive;
}
application.yml
spring.cache.redis:
time-to-live: 10m
app.cache:
configByName:
"[foo.bar]":
timeToLive: 0
"[foo.baz]":
timeToLive: 55s
Comment From: snicoll
@uqix good point but that part of the code is in Spring Data Redis (as the org.springframework.data
import hints you). Please open an issue in the Spring Data Redis issue tracker.
Comment From: mp911de
We were discussing this issue for quite a while. Throughout this discussion, the tuning aspect got lost and our focus got sidetracked to exposing the default configuration. My understanding is, that cache defaults are mostly okay, but the important bit here is to retain the default config for specific caches while wanting to tune some parameters. In any case, filing a Jira ticket in the Spring Date Redis issue tracker is the right place to continue that discussion.