Skip to content

Commit

Permalink
Parse redis database from url if present
Browse files Browse the repository at this point in the history
Fix GH-43810

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
  • Loading branch information
quaff committed Jan 14, 2025
1 parent e2a62d6 commit 2f51248
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
* @author Andy Wilkinson
* @author Phillip Webb
* @author Scott Frederick
* @author Yanming Zhou
*/
class PropertiesRedisConnectionDetails implements RedisConnectionDetails {

Expand All @@ -39,7 +40,7 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
@Override
public String getUsername() {
if (this.properties.getUrl() != null) {
ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl());
ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl());
return connectionInfo.getUsername();
}
return this.properties.getUsername();
Expand All @@ -48,7 +49,7 @@ public String getUsername() {
@Override
public String getPassword() {
if (this.properties.getUrl() != null) {
ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl());
ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl());
return connectionInfo.getPassword();
}
return this.properties.getPassword();
Expand All @@ -57,17 +58,14 @@ public String getPassword() {
@Override
public Standalone getStandalone() {
if (this.properties.getUrl() != null) {
ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl());
ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl());
return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(),
this.properties.getDatabase());
connectionInfo.getDatabase() != null ? connectionInfo.getDatabase()
: this.properties.getDatabase());
}
return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
}

private ConnectionInfo connectionInfo(String url) {
return (url != null) ? RedisConnectionConfiguration.parseUrl(url) : null;
}

@Override
public Sentinel getSentinel() {
org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel sentinel = this.properties
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,6 +45,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Yanming Zhou
*/
abstract class RedisConnectionConfiguration {

Expand Down Expand Up @@ -200,7 +201,12 @@ static ConnectionInfo parseUrl(String url) {
password = candidate;
}
}
return new ConnectionInfo(uri, useSsl, username, password);
Integer database = null;
String[] pathSplit = uri.getPath().split("/", 2);
if (pathSplit.length > 1 && !pathSplit[1].isEmpty()) {
database = Integer.parseInt(pathSplit[1]);
}
return new ConnectionInfo(uri, useSsl, username, password, database);
}
catch (URISyntaxException ex) {
throw new RedisUrlSyntaxException(url, ex);
Expand All @@ -217,11 +223,14 @@ static class ConnectionInfo {

private final String password;

ConnectionInfo(URI uri, boolean useSsl, String username, String password) {
private final Integer database;

ConnectionInfo(URI uri, boolean useSsl, String username, String password, Integer database) {
this.uri = uri;
this.useSsl = useSsl;
this.username = username;
this.password = password;
this.database = database;
}

URI getUri() {
Expand All @@ -240,6 +249,10 @@ String getPassword() {
return this.password;
}

Integer getDatabase() {
return this.database;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Mark Paluch
* @author Stephane Nicoll
* @author Scott Frederick
* @author Yanming Zhou
* @since 1.0.0
*/
@ConfigurationProperties(prefix = "spring.data.redis")
Expand All @@ -42,8 +43,8 @@ public class RedisProperties {
private int database = 0;

/**
* Connection URL. Overrides host, port, username, and password. Example:
* redis://user:password@example.com:6379
* Connection URL. Overrides host, port, username, password, and database. Example:
* redis://user:password@example.com:6379/8
*/
private String url;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void standaloneIsConfiguredFromUrl() {
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
assertThat(standalone.getHost()).isEqualTo("example.com");
assertThat(standalone.getPort()).isEqualTo(1234);
assertThat(standalone.getDatabase()).isEqualTo(5);
assertThat(standalone.getDatabase()).isEqualTo(9999);
}

@Test
Expand Down

0 comments on commit 2f51248

Please sign in to comment.