refactor: make session store redis by default

This commit is contained in:
lingniu
2026-07-01 10:31:26 +08:00
parent 53a9b3b3b7
commit 87830fee09
3 changed files with 19 additions and 11 deletions

View File

@@ -7,7 +7,7 @@ import java.time.Duration;
@ConfigurationProperties(prefix = "lingniu.ingest.session")
public class SessionProperties {
private Store store = Store.MEMORY;
private Store store = Store.REDIS;
private Duration ttl = Duration.ofMinutes(30);
@@ -16,6 +16,9 @@ public class SessionProperties {
}
public void setStore(Store store) {
if (store == Store.MEMORY) {
throw new IllegalStateException("session.store supports only redis; configured value: memory");
}
this.store = store;
}

View File

@@ -17,15 +17,10 @@ import org.springframework.data.redis.core.StringRedisTemplate;
@EnableConfigurationProperties(SessionProperties.class)
public class SessionCoreAutoConfiguration {
@Bean
@ConditionalOnProperty(prefix = "lingniu.ingest.session", name = "store", havingValue = "memory")
public Object rejectedLegacyMemorySessionStore() {
throw new IllegalStateException("session.store=memory has been removed; configure session.store=redis");
}
@Bean
@ConditionalOnBean(StringRedisTemplate.class)
@ConditionalOnProperty(prefix = "lingniu.ingest.session", name = "store", havingValue = "redis")
@ConditionalOnProperty(prefix = "lingniu.ingest.session", name = "store", havingValue = "redis",
matchIfMissing = true)
@ConditionalOnMissingBean
public SessionStore redisSessionStore(StringRedisTemplate redis, SessionProperties properties) {
// Redis 只保存会话元数据索引;真实 Netty Channel 仍在各协议进程本地内存中。

View File

@@ -43,12 +43,22 @@ class SessionCoreAutoConfigurationTest {
}
@Test
void rejectsLegacyMemorySessionStoreMode() {
void usesRedisSessionStoreByDefaultWhenRedisTemplateExists() {
contextRunner
.withBean(StringRedisTemplate.class, () -> mock(StringRedisTemplate.class))
.run(context -> {
assertThat(context).hasSingleBean(SessionStore.class);
assertThat(context).hasSingleBean(RedisSessionStore.class);
});
}
@Test
void rejectsMemorySessionStoreModeWithRedisOnlyRule() {
contextRunner
.withPropertyValues("lingniu.ingest.session.store=memory")
.run(context -> assertThat(context.getStartupFailure())
.hasRootCauseInstanceOf(IllegalStateException.class)
.hasMessageContaining("session.store=memory has been removed")
.hasMessageContaining("session.store=redis"));
.hasStackTraceContaining("session.store supports only redis")
.hasStackTraceContaining("memory"));
}
}