Skip to content

Commit

Permalink
Throw Exception on startup if bucket4j is enabled but no cache is con…
Browse files Browse the repository at this point in the history
…figured. #262
  • Loading branch information
MarcGiffing committed Mar 18, 2024
1 parent 651b000 commit bf6a2e6
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.giffing.bucket4j.spring.boot.starter;

import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.Bucket4jCacheConfiguration;
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import jakarta.annotation.Nullable;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.context.annotation.Configuration;


@Configuration
@AutoConfigureAfter( value = {
CacheAutoConfiguration.class,
Bucket4jCacheConfiguration.class
})
@RequiredArgsConstructor
public class Bucket4jStartupCheckConfiguration {


@Nullable
private final SyncCacheResolver syncCacheResolver;

@Nullable
private final AsyncCacheResolver asyncCacheResolver;


@PostConstruct
public void checkCache() {
// TODO check filter configuration, check method configuration
if(syncCacheResolver == null && asyncCacheResolver == null) {
// TODO throw proper exception
throw new IllegalStateException("cache not configured properly");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.giffing.bucket4j.spring.boot.starter.config.cache.Bucket4jCacheConfiguration;
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnBucket4jEnabled;
import com.giffing.bucket4j.spring.boot.starter.config.metrics.actuator.SpringBootActuatorConfig;
import com.giffing.bucket4j.spring.boot.starter.config.service.ServiceConfiguration;
import com.giffing.bucket4j.spring.boot.starter.context.RateLimiting;
Expand All @@ -12,7 +13,6 @@
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -22,8 +22,8 @@
* Enables the support for the {@link RateLimiting} annotation to rate limit on method level.
*/
@Configuration
@ConditionalOnBucket4jEnabled
@ConditionalOnClass(Aspect.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, value = {"enabled"}, matchIfMissing = true)
@EnableConfigurationProperties({Bucket4JBootProperties.class})
@AutoConfigureAfter(value = { CacheAutoConfiguration.class, Bucket4jCacheConfiguration.class })
@ConditionalOnBean(value = SyncCacheResolver.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheBucket4jConfiguration;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import com.hazelcast.core.HazelcastInstance;
Expand All @@ -21,7 +23,7 @@
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnClass({HazelcastInstance.class})
@ConditionalOnBean(HazelcastInstance.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "hazelcast-reactive", matchIfMissing = true)
@ConditionalOnCache("hazelcast-reactive")
public class HazelcastReactiveBucket4jCacheConfiguration {

private final HazelcastInstance hazelcastInstance;
Expand All @@ -41,14 +43,14 @@ public AsyncCacheResolver hazelcastCacheResolver() {

@Bean
@ConditionalOnMissingBean(CacheManager.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public CacheManager<String, Bucket4JConfiguration> configCacheManager() {
IMap<String, Bucket4JConfiguration> map = hazelcastInstance.getMap(configCacheName);
return new HazelcastCacheManager<>(map);
}

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public HazelcastCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
IMap<String, Bucket4JConfiguration> map = hazelcastInstance.getMap(configCacheName);
return new HazelcastCacheListener<>(map, eventPublisher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheBucket4jConfiguration;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import com.hazelcast.core.HazelcastInstance;
Expand All @@ -23,7 +25,7 @@
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass({HazelcastCacheManager.class})
@ConditionalOnBean(HazelcastCacheManager.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "hazelcast-spring", matchIfMissing = true)
@ConditionalOnCache("hazelcast-spring")
public class HazelcastSpringBucket4jCacheConfiguration {

private final HazelcastInstance hazelcastInstance;
Expand All @@ -42,14 +44,14 @@ public AsyncCacheResolver hazelcastCacheResolver() {

@Bean
@ConditionalOnMissingBean(CacheManager.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public CacheManager<String, Bucket4JConfiguration> configCacheManager() {
IMap<String, Bucket4JConfiguration> map = hazelcastInstance.getMap(configCacheName);
return new com.giffing.bucket4j.spring.boot.starter.config.cache.hazelcast.HazelcastCacheManager<>(map);
}

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public HazelcastCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
IMap<String, Bucket4JConfiguration> map = hazelcastInstance.getMap(configCacheName);
return new HazelcastCacheListener<>(map, eventPublisher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import org.apache.ignite.Ignite;
Expand All @@ -14,7 +16,7 @@
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnClass({ Ignite.class })
@ConditionalOnBean(Ignite.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "ignite", matchIfMissing = true)
@ConditionalOnCache("ignite")
public class IgniteBucket4jCacheConfiguration {

private final Ignite ignite;
Expand All @@ -33,13 +35,13 @@ public AsyncCacheResolver hazelcastCacheResolver() {

@Bean
@ConditionalOnMissingBean(CacheManager.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public CacheManager<String, Bucket4JConfiguration> configCacheManager() {
return new IgniteCacheManager<>(ignite.cache(configCacheName));
}

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public IgniteCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new IgniteCacheListener<>(ignite.cache(configCacheName), eventPublisher);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.giffing.bucket4j.spring.boot.starter.config.cache.AsyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import org.infinispan.manager.CacheContainer;
Expand All @@ -15,7 +17,7 @@
@ConditionalOnClass({CacheContainer.class})
@ConditionalOnBean(CacheContainer.class)
@ConditionalOnMissingBean(AsyncCacheResolver.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "infinispan", matchIfMissing = true)
@ConditionalOnCache("infinispan")
public class InfinispanBucket4jCacheConfiguration {

private final CacheContainer cacheContainer;
Expand All @@ -33,13 +35,13 @@ public AsyncCacheResolver infinispanCacheResolver() {

@Bean
@ConditionalOnMissingBean(CacheManager.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public CacheManager<String, Bucket4JConfiguration> configCacheManager() {
return new InfinispanCacheManager<>(cacheContainer.getCache(configCacheName));
}

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public InfinispanCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new InfinispanCacheListener<>(cacheContainer.getCache(configCacheName), eventPublisher);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.giffing.bucket4j.spring.boot.starter.config.cache.infinispan.InfinispanCacheListener;
import com.giffing.bucket4j.spring.boot.starter.config.cache.infinispan.InfinispanCacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.cache.infinispan.InfinispanCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import org.infinispan.manager.CacheContainer;
Expand All @@ -24,7 +26,7 @@
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass({CacheContainer.class, Caching.class, JCacheCacheManager.class})
@ConditionalOnBean(CacheContainer.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "jcache-ignite", matchIfMissing = true)
@ConditionalOnCache("jcache-ignite")
public class InfinispanJCacheBucket4jConfiguration {

private final CacheContainer cacheContainer;
Expand All @@ -43,13 +45,13 @@ public SyncCacheResolver bucket4jCacheResolver() {

@Bean
@ConditionalOnMissingBean(CacheManager.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public CacheManager<String, Bucket4JConfiguration> configCacheManager() {
return new InfinispanCacheManager<>(cacheContainer.getCache(configCacheName));
}

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public InfinispanCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new InfinispanCacheListener<>(cacheContainer.getCache(configCacheName), eventPublisher);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.giffing.bucket4j.spring.boot.starter.config.cache.jcache;

import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import org.springframework.boot.autoconfigure.condition.*;
Expand All @@ -16,35 +18,35 @@
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass({Caching.class, JCacheCacheManager.class})
@ConditionalOnBean(CacheManager.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "jcache", matchIfMissing = true)
@ConditionalOnCache("jcache")
public class JCacheBucket4jConfiguration {

private final CacheManager cacheManager;
private final String configCacheName;

public JCacheBucket4jConfiguration(CacheManager cacheManager, Bucket4JBootProperties properties) {
this.cacheManager = cacheManager;
this.configCacheName = properties.getFilterConfigCacheName();
}

@Bean
@ConditionalOnMissingBean(SyncCacheResolver.class)
public SyncCacheResolver bucket4jCacheResolver() {
return new JCacheCacheResolver(cacheManager);
}

@Bean
@ConditionalOnMissingBean(com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
public com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager<String, Bucket4JConfiguration> configCacheManager(
) {
return new com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheCacheManager<>(cacheManager.getCache(configCacheName));
}

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
public JCacheCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new JCacheCacheListener<>(cacheManager.getCache(configCacheName), eventPublisher);
}
private final CacheManager cacheManager;
private final String configCacheName;

public JCacheBucket4jConfiguration(CacheManager cacheManager, Bucket4JBootProperties properties) {
this.cacheManager = cacheManager;
this.configCacheName = properties.getFilterConfigCacheName();
}

@Bean
@ConditionalOnMissingBean(SyncCacheResolver.class)
public SyncCacheResolver bucket4jCacheResolver() {
return new JCacheCacheResolver(cacheManager);
}

@Bean
@ConditionalOnMissingBean(com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager.class)
@ConditionalOnFilterConfigCacheEnabled
public com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager<String, Bucket4JConfiguration> configCacheManager(
) {
return new com.giffing.bucket4j.spring.boot.starter.config.cache.jcache.JCacheCacheManager<>(cacheManager.getCache(configCacheName));
}

@Bean
@ConditionalOnFilterConfigCacheEnabled
public JCacheCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new JCacheCacheListener<>(cacheManager.getCache(configCacheName), eventPublisher);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.giffing.bucket4j.spring.boot.starter.config.cache.CacheManager;
import com.giffing.bucket4j.spring.boot.starter.config.cache.SyncCacheResolver;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnCache;
import com.giffing.bucket4j.spring.boot.starter.config.condition.ConditionalOnFilterConfigCacheEnabled;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JBootProperties;
import com.giffing.bucket4j.spring.boot.starter.context.properties.Bucket4JConfiguration;
import io.github.bucket4j.redis.jedis.cas.JedisBasedProxyManager.JedisBasedProxyManagerBuilder;
Expand All @@ -15,7 +17,7 @@
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(JedisBasedProxyManagerBuilder.class)
@ConditionalOnBean(JedisPool.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "cache-to-use", havingValue = "redis-jedis", matchIfMissing = true)
@ConditionalOnCache("redis-jedis")
public class JedisBucket4jConfiguration {

public final JedisPool jedisPool;
Expand All @@ -34,13 +36,13 @@ public SyncCacheResolver bucket4RedisResolver() {

@Bean
@ConditionalOnMissingBean(CacheManager.class)
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public CacheManager<String, Bucket4JConfiguration> configCacheManager() {
return new JedisCacheManager<>(jedisPool, configCacheName, Bucket4JConfiguration.class);
}

@Bean
@ConditionalOnProperty(prefix = Bucket4JBootProperties.PROPERTY_PREFIX, name = "filter-config-caching-enabled", havingValue = "true")
@ConditionalOnFilterConfigCacheEnabled
public JedisCacheListener<String, Bucket4JConfiguration> configCacheListener(ApplicationEventPublisher eventPublisher) {
return new JedisCacheListener<>(jedisPool, configCacheName, String.class, Bucket4JConfiguration.class, eventPublisher);
}
Expand Down
Loading

0 comments on commit bf6a2e6

Please sign in to comment.