io.github.dengchen2020.cache.caffeine.CaffeineCacheAutoConfiguration Maven / Gradle / Ivy
package io.github.dengchen2020.cache.caffeine;
import io.github.dengchen2020.cache.CacheHelper;
import io.github.dengchen2020.cache.properties.CacheSpecBuilder;
import io.github.dengchen2020.core.redis.RedisDependencyAutoConfiguration;
import io.github.dengchen2020.core.redis.RedisMessagePublisher;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
/**
* Caffeine缓存自动配置
*
* @author dengchen
* @since 2024/5/30
*/
@AutoConfigureAfter(RedisDependencyAutoConfiguration.class)
@EnableConfigurationProperties(CacheSpecBuilder.class)
@ConditionalOnProperty(value = "spring.cache.type", matchIfMissing = true, havingValue = "caffeine")
@Configuration(proxyBeanMethods = false)
public class CaffeineCacheAutoConfiguration {
@ConditionalOnBean(RedisMessagePublisher.class)
@ConditionalOnMissingBean(CacheHelper.class)
@Bean
public CaffeineCacheHelper caffeineCacheHelper(RedisMessagePublisher redisMessagePublisher){
return new CaffeineCacheHelper(redisMessagePublisher);
}
@ConditionalOnMissingBean(CacheManager.class)
@Bean
public CaffeineCacheManager caffeineCacheManager(CacheSpecBuilder cacheSpecBuilder,@Nullable CaffeineCacheHelper cacheHandler) {
return new CaffeineCacheManager(cacheSpecBuilder, cacheHandler);
}
}