com.makitoo.internal.CacheConfigurationProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feature-flag Show documentation
Show all versions of feature-flag Show documentation
The Java client for Makitoo feature handling.
The newest version!
package com.makitoo.internal;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.makitoo.User;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import static com.makitoo.internal.PropertiesUtils.getIntProp;
/**
* Created by nicolas on 24/01/17.
*/
public class CacheConfigurationProvider implements ConfigurationProvider {
private final LoadingCache cache;
public CacheConfigurationProvider(Properties properties, final ConfigurationProvider delegate) {
int maxSize = getIntProp(properties, "makitoo.cache.size", 1000);
int maxTime = getIntProp(properties, "makitoo.cache.time", 10 * 60 * 1000 /* 10 min */);
cache = CacheBuilder.newBuilder()
.maximumSize(maxSize)
.expireAfterWrite(maxTime, TimeUnit.MILLISECONDS)
.build(
new CacheLoader() {
public UserConfiguration load(User key) throws Exception {
return delegate.getUserConfiguration(key);
}
});
}
public UserConfiguration getUserConfiguration(User user) throws Exception {
return cache.get(user);
}
}