Skip to content
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

Closed
snicoll opened this issue Jan 20, 2020 · 4 comments
Closed

Document RedisCacheManagerBuilderCustomizer #19813

snicoll opened this issue Jan 20, 2020 · 4 comments
Labels
status: superseded An issue that has been superseded by another type: documentation A documentation update

Comments

@snicoll
Copy link
Member

snicoll commented Jan 20, 2020

See #15960 (comment)

@snicoll snicoll added the type: documentation A documentation update label Jan 20, 2020
@snicoll snicoll added this to the 2.2.x milestone Jan 20, 2020
nosan added a commit to nosan/spring-boot that referenced this issue Jan 20, 2020
@snicoll
Copy link
Member Author

snicoll commented Jan 20, 2020

Closing in favour of PR #19819

@snicoll snicoll closed this as completed Jan 20, 2020
@snicoll snicoll added the status: superseded An issue that has been superseded by another label Jan 20, 2020
@snicoll snicoll removed this from the 2.2.x milestone Jan 20, 2020
@uqix
Copy link

uqix commented Jan 21, 2020

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

@snicoll
Copy link
Member Author

snicoll commented Jan 21, 2020

@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.

@mp911de
Copy link
Member

mp911de commented Feb 7, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants