Polish "Add session property for ConfigureRedisAction"

See gh-17022
pull/17129/head
Phillip Webb 6 years ago
parent a0fabc9851
commit 919913ae66

@ -22,7 +22,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.session.RedisSessionProperties.ConfigurationStrategy;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
@ -52,6 +51,19 @@ import org.springframework.session.data.redis.config.annotation.web.http.RedisHt
@EnableConfigurationProperties(RedisSessionProperties.class)
class RedisSessionConfiguration {
@Bean
@ConditionalOnMissingBean
public ConfigureRedisAction configureRedisAction(RedisSessionProperties redisSessionProperties) {
switch (redisSessionProperties.getConfigurationStrategy()) {
case NOTIFY_KEYSPACE_EVENTS:
return new ConfigureNotifyKeyspaceEventsAction();
case NONE:
return ConfigureRedisAction.NO_OP;
}
throw new IllegalStateException("Unsupported redis configuration strategy '"
+ redisSessionProperties.getConfigurationStrategy() + "'.");
}
@Configuration
public static class SpringBootRedisHttpSessionConfiguration extends RedisHttpSessionConfiguration {
@ -66,19 +78,6 @@ class RedisSessionConfiguration {
setCleanupCron(redisSessionProperties.getCleanupCron());
}
@Bean
@ConditionalOnMissingBean
public ConfigureRedisAction configureRedisAction(RedisSessionProperties redisSessionProperties) {
ConfigurationStrategy strategy = redisSessionProperties.getConfigurationStrategy();
if (strategy == ConfigurationStrategy.NOTIFY_KEYSPACE_EVENTS) {
return new ConfigureNotifyKeyspaceEventsAction();
}
if (strategy == ConfigurationStrategy.NO_OP) {
return ConfigureRedisAction.NO_OP;
}
throw new IllegalStateException("Strategy '" + strategy + "' is not supported.");
}
}
}

@ -41,9 +41,10 @@ public class RedisSessionProperties {
private RedisFlushMode flushMode = RedisFlushMode.ON_SAVE;
/**
* Allows specifying a strategy for configuring and validating Redis.
* The configure action to apply when no user defined ConfigureRedisAction bean is
* present.
*/
private ConfigurationStrategy configurationStrategy = ConfigurationStrategy.NOTIFY_KEYSPACE_EVENTS;
private ConfigureAction configureAction = ConfigureAction.NOTIFY_KEYSPACE_EVENTS;
/**
* Cron expression for expired session cleanup job.
@ -74,28 +75,29 @@ public class RedisSessionProperties {
this.cleanupCron = cleanupCron;
}
public ConfigurationStrategy getConfigurationStrategy() {
return this.configurationStrategy;
public ConfigureAction getConfigurationStrategy() {
return this.configureAction;
}
public void setConfigurationStrategy(ConfigurationStrategy configurationStrategy) {
this.configurationStrategy = configurationStrategy;
public void setConfigurationStrategy(ConfigureAction configurationStrategy) {
this.configureAction = configurationStrategy;
}
/**
* Allows specifying a strategy for configuring and validating Redis.
* Strategies for configuring and validating Redis.
*/
public enum ConfigurationStrategy {
public enum ConfigureAction {
/**
* Do nothing.
* Ensure that Redis Keyspace events for Generic commands and Expired events are
* enabled.
*/
NO_OP,
NOTIFY_KEYSPACE_EVENTS,
/**
* Ensures that Redis Keyspace events for Generic commands and Expired events are
* enabled.
* No not attempt to apply any custom Redis configuration.
*/
NOTIFY_KEYSPACE_EVENTS
NONE
}

@ -91,8 +91,7 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
@Test
void redisSessionConfigureNoStrategy() {
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
.withPropertyValues("spring.session.store-type=redis",
"spring.session.redis.configuration-strategy=no_op",
.withPropertyValues("spring.session.store-type=redis", "spring.session.redis.configure-action=none",
"spring.redis.port=" + redis.getFirstMappedPort())
.run(validateStrategy(ConfigureRedisAction.NO_OP.getClass()));
}
@ -130,13 +129,14 @@ class SessionAutoConfigurationRedisTests extends AbstractSessionAutoConfiguratio
}
private ContextConsumer<AssertableWebApplicationContext> validateStrategy(
Class<? extends ConfigureRedisAction> actionClass, Map.Entry... values) {
Class<? extends ConfigureRedisAction> expectedConfigureRedisActionType, Map.Entry<?, ?>... expectedConfig) {
return (context) -> {
assertThat(context).hasSingleBean(ConfigureRedisAction.class).hasSingleBean(RedisConnectionFactory.class);
assertThat(context.getBean(ConfigureRedisAction.class)).isInstanceOf(actionClass);
assertThat(context).hasSingleBean(ConfigureRedisAction.class);
assertThat(context).hasSingleBean(RedisConnectionFactory.class);
assertThat(context.getBean(ConfigureRedisAction.class)).isInstanceOf(expectedConfigureRedisActionType);
RedisConnection connection = context.getBean(RedisConnectionFactory.class).getConnection();
if (values.length > 0) {
assertThat(connection.getConfig("*")).contains(values);
if (expectedConfig.length > 0) {
assertThat(connection.getConfig("*")).contains(expectedConfig);
}
};
}

Loading…
Cancel
Save