org.zodiac.autoconfigure.loadbalancer.AppLoadBalancerCacheAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.loadbalancer;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.zodiac.core.loadbalancer.cache.CaffeineAppLoadBalancerCacheManager;
import org.zodiac.core.loadbalancer.cache.DefaultAppLoadBalancerCacheManager;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnSpringCloudBootstrapDisabled;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.core.loadbalancer.cache.AppLoadBalancerCacheManager;
@SpringBootConfiguration
@ConditionalOnSpringCloudBootstrapDisabled
@AutoConfigureAfter(value = {org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration.class})
//@EnableConfigurationProperties(AppLoadBalancerCacheProperties.class)
@ConditionalOnClass(value = {org.springframework.cache.CacheManager.class, org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration.class})
@ConditionalOnProperty(prefix = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CACHE_PREFIX, value = "enabled", havingValue = "true", matchIfMissing = true)
public class AppLoadBalancerCacheAutoConfiguration {
@Bean
@ConfigurationProperties(prefix = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_LOADBALANCER_CACHE_PREFIX)
protected AppLoadBalancerCacheProperties appLoadBalancerCacheProperties() {
return new AppLoadBalancerCacheProperties();
}
@SpringBootConfiguration
@Conditional(value = {OnCaffeineCacheMissingCondition.class})
protected static class LoadBalancerCacheManagerWarnConfiguration {
@Bean
AppLoadBalancerCaffeineWarnLogger caffeineWarnLogger() {
return new AppLoadBalancerCaffeineWarnLogger();
}
}
static class AppLoadBalancerCaffeineWarnLogger {
private static final Logger LOG = LoggerFactory.getLogger(AppLoadBalancerCaffeineWarnLogger.class);
@PostConstruct
void logWarning() {
if (LOG.isWarnEnabled()) {
LOG.warn("Application loadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and `org.springframework.cache.caffeine.CaffeineCacheManager` to the classpath.");
}
}
}
@SpringBootConfiguration
@ConditionalOnClass(value = {com.github.benmanes.caffeine.cache.Caffeine.class, org.springframework.cache.caffeine.CaffeineCacheManager.class})
protected static class CaffeineLoadBalancerCacheManagerConfiguration {
@Bean(autowireCandidate = false)
@ConditionalOnMissingBean
AppLoadBalancerCacheManager caffeineLoadBalancerCacheManager(AppLoadBalancerCacheProperties cacheProperties) {
return new CaffeineAppLoadBalancerCacheManager(cacheProperties);
}
}
@SpringBootConfiguration
@Conditional(value = {OnCaffeineCacheMissingCondition.class})
@ConditionalOnClass(value = {com.stoyanr.evictor.ConcurrentMapWithTimedEviction.class})
protected static class DefaultLoadBalancerCacheManagerConfiguration {
@Bean(autowireCandidate = false)
@ConditionalOnMissingBean
AppLoadBalancerCacheManager defaultLoadBalancerCacheManager(AppLoadBalancerCacheProperties cacheProperties) {
return new DefaultAppLoadBalancerCacheManager(cacheProperties);
}
}
static final class OnCaffeineCacheMissingCondition extends AnyNestedCondition {
private OnCaffeineCacheMissingCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnMissingClass(value = {"com.github.benmanes.caffeine.cache.Caffeine"})
static class CaffeineClassMissing {
}
@ConditionalOnMissingClass(value = {"org.springframework.cache.caffeine.CaffeineCacheManager"})
static class CaffeineCacheManagerClassMissing {
}
}
}