Skip to content

Commit e4bcda2

Browse files
philwebbsnicoll
andcommitted
Polish "Add spring.data.redis.lettuce.read-from property"
See gh-42588 Co-authored-by: Stephane Nicoll <[email protected]>
1 parent fd11598 commit e4bcda2

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,25 @@ private void applyProperties(LettuceClientConfiguration.LettuceClientConfigurati
174174
}
175175
}
176176

177-
private static ReadFrom getReadFrom(String readFrom) {
177+
private ReadFrom getReadFrom(String readFrom) {
178178
int index = readFrom.indexOf(':');
179179
if (index == -1) {
180-
String name = readFrom.replaceAll("-", "");
181-
return ReadFrom.valueOf(name);
180+
return ReadFrom.valueOf(getCanonicalReadFromName(readFrom));
182181
}
183-
String name = readFrom.substring(0, index).replaceAll("-", "");
182+
String name = getCanonicalReadFromName(readFrom.substring(0, index));
184183
String value = readFrom.substring(index + 1);
185184
return ReadFrom.valueOf(name + ":" + value);
186185
}
187186

187+
private String getCanonicalReadFromName(String name) {
188+
StringBuilder canonicalName = new StringBuilder(name.length());
189+
name.chars()
190+
.filter(Character::isLetterOrDigit)
191+
.map(Character::toLowerCase)
192+
.forEach((c) -> canonicalName.append((char) c));
193+
return canonicalName.toString();
194+
}
195+
188196
private ClientOptions createClientOptions(
189197
ObjectProvider<LettuceClientOptionsBuilderCustomizer> clientConfigurationBuilderCustomizers) {
190198
ClientOptions.Builder builder = initializeClientOptionsBuilder();

Diff for: spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -122,7 +122,7 @@ void testOverrideRedisConfiguration() {
122122
});
123123
}
124124

125-
@ParameterizedTest
125+
@ParameterizedTest(name = "{0}")
126126
@MethodSource
127127
void shouldConfigureLettuceReadFromProperty(String type, ReadFrom readFrom) {
128128
this.contextRunner.withPropertyValues("spring.data.redis.lettuce.read-from:" + type).run((context) -> {
@@ -132,6 +132,14 @@ void shouldConfigureLettuceReadFromProperty(String type, ReadFrom readFrom) {
132132
});
133133
}
134134

135+
static Stream<Arguments> shouldConfigureLettuceReadFromProperty() {
136+
return Stream.of(Arguments.of("any", ReadFrom.ANY), Arguments.of("any-replica", ReadFrom.ANY_REPLICA),
137+
Arguments.of("lowest-latency", ReadFrom.LOWEST_LATENCY), Arguments.of("replica", ReadFrom.REPLICA),
138+
Arguments.of("replica-preferred", ReadFrom.REPLICA_PREFERRED),
139+
Arguments.of("upstream", ReadFrom.UPSTREAM),
140+
Arguments.of("upstream-preferred", ReadFrom.UPSTREAM_PREFERRED));
141+
}
142+
135143
@Test
136144
void shouldConfigureLettuceRegexReadFromProperty() {
137145
RedisClusterNode node1 = createRedisNode("redis-node-1.region-1.example.com");
@@ -688,14 +696,6 @@ private String getUserName(LettuceConnectionFactory factory) {
688696
return ReflectionTestUtils.invokeMethod(factory, "getRedisUsername");
689697
}
690698

691-
static Stream<Arguments> shouldConfigureLettuceReadFromProperty() {
692-
return Stream.of(Arguments.of("any", ReadFrom.ANY), Arguments.of("any-replica", ReadFrom.ANY_REPLICA),
693-
Arguments.of("lowest-latency", ReadFrom.LOWEST_LATENCY), Arguments.of("replica", ReadFrom.REPLICA),
694-
Arguments.of("replica-preferred", ReadFrom.REPLICA_PREFERRED),
695-
Arguments.of("upstream", ReadFrom.UPSTREAM),
696-
Arguments.of("upstream-preferred", ReadFrom.UPSTREAM_PREFERRED));
697-
}
698-
699699
private RedisClusterNode createRedisNode(String host) {
700700
RedisClusterNode node = new RedisClusterNode();
701701
node.setUri(RedisURI.Builder.redis(host).build());

0 commit comments

Comments
 (0)