Skip to content

Commit

Permalink
Redis passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Sep 25, 2020
1 parent eb0ca46 commit aca650b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.14.2</version>
<version>2.14.3</version>
</dependency>
</dependencies>

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/mengcraft/simpleorm/ORM.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.SneakyThrows;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;
Expand Down Expand Up @@ -79,9 +80,10 @@ public void onEnable() {
new MetricsLite(this);
if (nil(globalRedisWrapper)) {
if (redisProvider == null) {
String redisUrl = getConfig().getString("redis.url", "");
int max = getConfig().getInt("redis.max_conn", -1);
redisProvider = RedisProviders.of(getConfig().getString("redis.master_name"), redisUrl, max);
FileConfiguration config = getConfig();
String redisUrl = config.getString("redis.url", "");
int max = config.getInt("redis.max_conn", -1);
redisProvider = RedisProviders.of(config.getString("redis.master_name"), redisUrl, max, config.getString("redis.password"));
}
globalRedisWrapper = new RedisWrapper(redisProvider);
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/mengcraft/simpleorm/redis/RedisProviders.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mengcraft.simpleorm.redis;

import com.mengcraft.simpleorm.lib.Utils;
import com.mengcraft.simpleorm.provider.IRedisProvider;
import lombok.RequiredArgsConstructor;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
Expand All @@ -14,13 +15,16 @@

public class RedisProviders {

public static IRedisProvider of(String sentinel, String url, int conn) {
public static IRedisProvider of(String sentinel, String url, int conn, String password) {
GenericObjectPoolConfig<?> config = new GenericObjectPoolConfig<>();
if (conn >= 1) {
config.setMaxTotal(conn);
}
if (sentinel == null || sentinel.isEmpty()) {
return new GenericProvider(new JedisPool(config, URI.create(url)));
if (Utils.isNullOrEmpty(sentinel)) {
URI remote = URI.create(url);
return new GenericProvider(Utils.isNullOrEmpty(password) ?
new JedisPool(config, remote) :
new JedisPool(config, remote.getHost(), remote.getPort(), 2000, password));
}
Set<String> sentinels = new HashSet<>();
if (url.matches("redis://(.+[,].+)")) {
Expand All @@ -32,7 +36,9 @@ public static IRedisProvider of(String sentinel, String url, int conn) {
sentinels.add(uri.getHost() + ':' + uri.getPort());
}
}
return new SentinelProvider(new JedisSentinelPool(sentinel, sentinels, config));
return new SentinelProvider(Utils.isNullOrEmpty(password) ?
new JedisSentinelPool(sentinel, sentinels, config) :
new JedisSentinelPool(sentinel, sentinels, config, password));
}

@RequiredArgsConstructor
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ redis:
master_name: null
url: redis://localhost:6379
max_conn: 20
password: null

mongo:
url: mongodb://localhost

0 comments on commit aca650b

Please sign in to comment.