Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
SunYu committed Nov 1, 2024
1 parent 1a06cf0 commit ff2b7db
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<dependency>
<groupId>sunyu.util</groupId>
<artifactId>util-redis</artifactId>
<version>lettuce-6.4.0.RELEASE_v1.0</version>
<version>lettuce-6.5.0.RELEASE_v1.0</version>
</dependency>
```

Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>sunyu.util</groupId>
<artifactId>util-redis</artifactId>
<version>lettuce-6.4.0.RELEASE_v1.0</version>
<version>lettuce-6.5.0.RELEASE_v1.0</version>

<properties>
<java.version>1.8</java.version>
Expand Down Expand Up @@ -34,19 +34,19 @@
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.31</version>
<version>5.8.32</version>
</dependency>
<!-- https://central.sonatype.com/artifact/io.lettuce/lettuce-core/versions -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.4.0.RELEASE</version>
<version>6.5.0.RELEASE</version>
</dependency>
<!-- https://central.sonatype.com/artifact/org.junit.jupiter/junit-jupiter/versions -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.3</version>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -124,7 +124,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
<version>3.5.1</version>
<configuration>
<skip>true</skip>
</configuration>
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/sunyu/util/RedisUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
* @author 孙宇
*/
public class RedisUtil implements Serializable, Closeable {
private Log log = LogFactory.get();
private final Log log = LogFactory.get();
private static final RedisUtil INSTANCE = new RedisUtil();


private volatile Map<String, RedisClient> clientMap = new HashMap<>();
private volatile Map<String, RedisClusterClient> clusterClientMap = new HashMap<>();
private volatile Map<String, StatefulRedisConnection<String, String>> standaloneConnectionMap = new HashMap<>();
private volatile Map<String, StatefulRedisMasterReplicaConnection<String, String>> sentinelConnectionMap = new HashMap<>();
private volatile Map<String, StatefulRedisClusterConnection<String, String>> clusterConnectionMap = new HashMap<>();

private final Map<String, RedisClient> clientMap = new HashMap<>();
private final Map<String, RedisClusterClient> clusterClientMap = new HashMap<>();
private final Map<String, StatefulRedisConnection<String, String>> standaloneConnectionMap = new HashMap<>();
private final Map<String, StatefulRedisMasterReplicaConnection<String, String>> sentinelConnectionMap = new HashMap<>();
private final Map<String, StatefulRedisClusterConnection<String, String>> clusterConnectionMap = new HashMap<>();

/**
* 获取standalone命令对象
Expand Down Expand Up @@ -121,7 +120,7 @@ public StatefulRedisClusterConnection<String, String> cluster(List<String> uris)
return clusterConnectionMap.get(urisStr);
}

List<RedisURI> uriList = new ArrayList();
List<RedisURI> uriList = new ArrayList<>();
for (String uri : uris) {
uriList.add(RedisURI.create(uri));
}
Expand Down
60 changes: 50 additions & 10 deletions src/test/java/sunyu/util/test/TestRedisUtil.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package sunyu.util.test;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import io.lettuce.core.KeyValue;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.cluster.RedisClusterClient;
Expand All @@ -18,30 +16,38 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

public class TestRedisUtil {
Log log = LogFactory.get();

/**
* 单节点模式
*/
@Test
void t001() {
void testStandalone() {
// Syntax: redis://[password@]host[:port][/databaseNumber]
// Syntax: redis://[username:password@]host[:port][/databaseNumber]
RedisClient redisClient = RedisClient.create("redis://192.168.11.39:16379/0");
StatefulRedisConnection<String, String> connection = redisClient.connect();
log.debug("Connected to Redis");
RedisCommands<String, String> redisCommands = connection.sync();

// todo
log.debug(redisCommands.get("subsidy:bc:userinfo"));

connection.close();
redisClient.shutdown();
}

/**
* 集群模式
*/
@Test
void t002() {
void testCluster() {
// Syntax: redis://[password@]host[:port]
// Syntax: redis://[username:password@]host[:port]
List<RedisURI> uris = new ArrayList();
List<RedisURI> uris = new ArrayList<>();
uris.add(RedisURI.create("redis://192.168.11.124:7001"));
uris.add(RedisURI.create("redis://192.168.11.124:7002"));
uris.add(RedisURI.create("redis://192.168.11.124:7003"));
Expand All @@ -53,6 +59,7 @@ void t002() {
log.debug("Connected to Redis Cluster");
RedisAdvancedClusterCommands<String, String> redisClusterCommands = connection.sync();

// todo
for (KeyValue<String, String> kv : redisClusterCommands.mget("relation:16200442", "farm:realtime:0865306056453850")) {
log.debug("{} {}", kv.getKey(), kv.getValue());
}
Expand All @@ -61,21 +68,31 @@ void t002() {
redisClient.shutdown();
}

/**
* 哨兵模式
*/
@Test
void t003() {
void testSentinel() {
// Syntax: redis-sentinel://[password@]host[:port][,host2[:port2]][/databaseNumber]#sentinelMasterId
RedisClient redisClient = RedisClient.create("redis-sentinel://localhost:26379,localhost:26380/0#mymaster");
StatefulRedisConnection<String, String> connection = redisClient.connect();
log.debug("Connected to Redis using Redis Sentinel");
RedisCommands<String, String> redisSentinelCommands = connection.sync();

// todo
String v = redisSentinelCommands.get("key");
log.info(v);

connection.close();
redisClient.shutdown();
}


/**
* get命令
*/
@Test
void t004() {
void testGet() {
RedisUtil redisUtil = RedisUtil.builder().build();//全局只需要一个
StatefulRedisConnection<String, String> standalone = redisUtil.standalone("redis://192.168.11.39:16379/0");//全局只需要一个

Expand All @@ -86,7 +103,7 @@ void t004() {
}

@Test
void t005() {
void testMget() {
RedisUtil redisUtil = RedisUtil.builder().build();//全局只需要一个
StatefulRedisClusterConnection<String, String> cluster = redisUtil.cluster(
Arrays.asList("redis://192.168.11.124:7001", "redis://192.168.11.124:7002", "redis://192.168.11.124:7003",
Expand All @@ -104,6 +121,29 @@ void t005() {
redisUtil.close();//如果程序不再使用了,可以调用这个
}

@Test
void testKeys() {
String nodes = "192.168.11.124:7001,192.168.11.124:7002,192.168.11.124:7003,192.168.11.125:7004,192.168.11.125:7005,192.168.11.125:7006";
RedisUtil redisUtil = RedisUtil.builder().build();//全局只需要一个
StatefulRedisClusterConnection<String, String> cluster = redisUtil
.cluster(Arrays.stream(nodes.split(","))
.map(s -> s.split(":"))
.map(arr -> StrUtil.format("redis://{}:{}", arr[0], arr[1]))
.collect(Collectors.toList()));
RedisAdvancedClusterCommands<String, String> sync = cluster.sync();

KeyScanCursor<String> scanCursor = null;
ScanArgs scanArgs = new ScanArgs().match("ne:realtime:*").limit(500);
do {
scanCursor = (scanCursor == null) ? sync.scan(scanArgs) : sync.scan(scanCursor, scanArgs);
for (String key : scanCursor.getKeys()) {
log.info("{}", key);
}
} while (!scanCursor.isFinished());

redisUtil.close();
}

@Test
void t006() {
RedisUtil redisUtil = RedisUtil.builder().build();//全局只需要一个
Expand Down

0 comments on commit ff2b7db

Please sign in to comment.