-
Notifications
You must be signed in to change notification settings - Fork 40.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document RedisCacheManagerBuilderCustomizer #19813
Comments
Closing in favour of PR #19819 |
FYI, here is my cache config code: I want to point out:
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 |
@uqix good point but that part of the code is in Spring Data Redis (as the |
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. |
See #15960 (comment)
The text was updated successfully, but these errors were encountered: