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

WIP: CaffeineCacheManager: Allow configuration of custom caches #1932

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
* {@link CacheManager} implementation that lazily builds {@link CaffeineCache}
Expand Down Expand Up @@ -59,7 +60,9 @@ public class CaffeineCacheManager implements CacheManager {

private boolean dynamic = true;

private Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder();
private final ConcurrentMap<String, Caffeine> cacheBuilder = new ConcurrentHashMap<>(16);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we shouldn't put that outside of the CacheManager business. After all if you want to configure your infrastructure, that could/should happen upfront. I think this would also vastly simplify the current arrangement where we have to manage the state if the setter is called once some caches are already being built.


private Caffeine<Object, Object> defaultCacheBuilder = Caffeine.newBuilder();

@Nullable
private CacheLoader<Object, Object> cacheLoader;
Expand Down Expand Up @@ -109,8 +112,18 @@ public void setCacheNames(@Nullable Collection<String> cacheNames) {
* @see com.github.benmanes.caffeine.cache.Caffeine#build()
*/
public void setCaffeine(Caffeine<Object, Object> caffeine) {
setCaffeine(null, caffeine);
}

/**
* Set the Caffeine to use for building the
* {@link CaffeineCache} instance with the given name.
* @see #createNativeCaffeineCache
* @see com.github.benmanes.caffeine.cache.Caffeine#build()
*/
public void setCaffeine(String name, Caffeine<Object, Object> caffeine) {
Assert.notNull(caffeine, "Caffeine must not be null");
doSetCaffeine(caffeine);
doSetCaffeine(name, caffeine);
}

/**
Expand All @@ -120,7 +133,17 @@ public void setCaffeine(Caffeine<Object, Object> caffeine) {
* @see com.github.benmanes.caffeine.cache.Caffeine#from(CaffeineSpec)
*/
public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
doSetCaffeine(Caffeine.from(caffeineSpec));
setCaffeineSpec(null, caffeineSpec);
}

/**
* Set the {@link CaffeineSpec} to use for building the
* {@link CaffeineCache} instance with the given name.
* @see #createNativeCaffeineCache
* @see com.github.benmanes.caffeine.cache.Caffeine#from(CaffeineSpec)
*/
public void setCaffeineSpec(String name, CaffeineSpec caffeineSpec) {
doSetCaffeine(name, Caffeine.from(caffeineSpec));
}

/**
Expand All @@ -131,7 +154,18 @@ public void setCaffeineSpec(CaffeineSpec caffeineSpec) {
* @see com.github.benmanes.caffeine.cache.Caffeine#from(String)
*/
public void setCacheSpecification(String cacheSpecification) {
doSetCaffeine(Caffeine.from(cacheSpecification));
setCacheSpecification(null, cacheSpecification);
}

/**
* Set the Caffeine cache specification String to use for building the
* {@link CaffeineCache} instance with the given name. The given value needs to
* comply with Caffeine's {@link CaffeineSpec} (see its javadoc).
* @see #createNativeCaffeineCache
* @see com.github.benmanes.caffeine.cache.Caffeine#from(String)
*/
public void setCacheSpecification(String name, String cacheSpecification) {
doSetCaffeine(name, Caffeine.from(cacheSpecification));
}

/**
Expand Down Expand Up @@ -206,17 +240,32 @@ protected Cache createCaffeineCache(String name) {
* @return the native Caffeine Cache instance
*/
protected com.github.benmanes.caffeine.cache.Cache<Object, Object> createNativeCaffeineCache(String name) {
if(cacheBuilder.containsKey(name)){
return createNativeCaffeineCache(cacheBuilder.get(name));
}
else{
return createNativeCaffeineCache(defaultCacheBuilder);
}
}

private com.github.benmanes.caffeine.cache.Cache<Object, Object> createNativeCaffeineCache(Caffeine builder){
if (this.cacheLoader != null) {
return this.cacheBuilder.build(this.cacheLoader);
return builder.build(this.cacheLoader);
}
else {
return this.cacheBuilder.build();
return builder.build();
}
}

private void doSetCaffeine(Caffeine<Object, Object> cacheBuilder) {
if (!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) {
this.cacheBuilder = cacheBuilder;
private void doSetCaffeine(String name, Caffeine<Object, Object> cacheBuilder) {
if(StringUtils.isEmpty(name)){
if (!ObjectUtils.nullSafeEquals(this.defaultCacheBuilder, cacheBuilder)) {
this.defaultCacheBuilder = cacheBuilder;
refreshKnownCaches();
}
}
else if(!ObjectUtils.nullSafeEquals(this.cacheBuilder.get(name), cacheBuilder)) {
this.cacheBuilder.put(name, cacheBuilder);
refreshKnownCaches();
}
}
Expand Down