From 4ca1510d2fd52cc0847f004e29ee8fa7d89efbb0 Mon Sep 17 00:00:00 2001
From: 28810 <28810@YEXIANGQIN>
Date: Wed, 25 Dec 2019 13:23:42 +0800
Subject: [PATCH] =?UTF-8?q?-=20ipv6=20=E5=8F=82=E6=95=B0=E8=A7=A3=E6=9E=90?=
=?UTF-8?q?=E7=BA=A0=E6=AD=A3=20#228?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 2 ++
src/CSRedisCore.csproj | 2 +-
src/RedisClientPool.cs | 29 +++++++++++++------
.../CSRedisClientHashTests.cs | 8 ++---
.../CSRedisClientStringTests.cs | 2 +-
5 files changed, 28 insertions(+), 15 deletions(-)
diff --git a/README.md b/README.md
index 0bb4b6d..3e2a0b9 100644
--- a/README.md
+++ b/README.md
@@ -53,6 +53,8 @@ var csredis = new CSRedis.CSRedisClient("127.0.0.1:6379,password=123,defaultData
| name | <空> | 连接名称,可以使用 Client List 命令查看 |
| prefix | <空> | key前辍,所有方法都会附带此前辍,csredis.Set(prefix + "key", 111); |
+> IPv6 格式:[fe80::b164:55b3:4b4f:7ce6%15]:6379
+
# 哨兵模式
```csharp
diff --git a/src/CSRedisCore.csproj b/src/CSRedisCore.csproj
index e25b1b3..4c63b50 100644
--- a/src/CSRedisCore.csproj
+++ b/src/CSRedisCore.csproj
@@ -4,7 +4,7 @@
CSRedisCore
CSRedisCore
CSRedisCore
- 3.2.1
+ 3.3.0
true
https://github.com/2881099/csredis
CSRedis 是 redis.io 官方推荐库,支持 redis-trib集群、哨兵、私有分区与连接池管理技术,简易 RedisHelper 静态类。
diff --git a/src/RedisClientPool.cs b/src/RedisClientPool.cs
index 91499dc..5e862a6 100644
--- a/src/RedisClientPool.cs
+++ b/src/RedisClientPool.cs
@@ -116,34 +116,45 @@ public class RedisClientPoolPolicy : IPolicy
internal void SetHost(string host)
{
+ if (string.IsNullOrEmpty(host?.Trim())) {
+ _ip = "127.0.0.1";
+ _port = 6379;
+ return;
+ }
+ host = host.Trim();
+ var ipv6 = Regex.Match(host, @"^\[([^\]]+)\]\s*(:\s*(\d+))?$");
+ if (ipv6.Success) //ipv6+port 格式: [fe80::b164:55b3:4b4f:7ce6%15]:6379
+ {
+ _ip = ipv6.Groups[1].Value.Trim();
+ _port = int.TryParse(ipv6.Groups[3].Value, out var tryint) && tryint > 0 ? tryint : 6379;
+ return;
+ }
var spt = (host ?? "").Split(':');
- if (spt.Length == 1)
+ if (spt.Length == 1) //ipv4 or domain
{
_ip = string.IsNullOrEmpty(spt[0].Trim()) == false ? spt[0].Trim() : "127.0.0.1";
_port = 6379;
return;
}
- if (spt.Length == 2)
+ if (spt.Length == 2) //ipv4:port or domain:port
{
if (int.TryParse(spt.Last().Trim(), out var testPort2))
{
_ip = string.IsNullOrEmpty(spt[0].Trim()) == false ? spt[0].Trim() : "127.0.0.1";
_port = testPort2;
+ return;
}
- else
- {
- _ip = host;
- _port = 6379;
- }
+ _ip = host;
+ _port = 6379;
return;
}
- if (IPAddress.TryParse(host, out var tryip) && tryip.AddressFamily == AddressFamily.InterNetworkV6)
+ if (IPAddress.TryParse(host, out var tryip) && tryip.AddressFamily == AddressFamily.InterNetworkV6) //test ipv6
{
_ip = host;
_port = 6379;
return;
}
- if (int.TryParse(spt.Last().Trim(), out var testPort))
+ if (int.TryParse(spt.Last().Trim(), out var testPort)) //test ipv6:port
{
var testHost = string.Join(":", spt.Where((a, b) => b < spt.Length - 1));
if (IPAddress.TryParse(testHost, out tryip) && tryip.AddressFamily == AddressFamily.InterNetworkV6)
diff --git a/test/CSRedisCore.Tests/CSRedisClientHashTests.cs b/test/CSRedisCore.Tests/CSRedisClientHashTests.cs
index 2601e4b..6d46ac4 100644
--- a/test/CSRedisCore.Tests/CSRedisClientHashTests.cs
+++ b/test/CSRedisCore.Tests/CSRedisClientHashTests.cs
@@ -76,12 +76,12 @@ public void HIncrBy() {
[Fact]
public void HIncrByFloat() {
Assert.True(rds.HMSet("TestHIncrByFloat", "null1", base.Null, "string1", base.String, "bytes1", base.Bytes, "class1", base.Class, "class1array", new[] { base.Class, base.Class }));
- Assert.Equal(0.5, rds.HIncrByFloat("TestHIncrByFloat", "null1", 0.5));
- Assert.Throws(() => rds.HIncrByFloat("TestHIncrByFloat", "string1", 1.5));
+ Assert.Equal(0.5m, rds.HIncrByFloat("TestHIncrByFloat", "null1", 0.5m));
+ Assert.Throws(() => rds.HIncrByFloat("TestHIncrByFloat", "string1", 1.5m));
Assert.Throws(() => rds.HIncrByFloat("TestHIncrByFloat", "bytes1", 5));
- Assert.Equal(3.8, rds.HIncrByFloat("TestHIncrByFloat", "null1", 3.3));
- Assert.Equal(14.3, rds.HIncrByFloat("TestHIncrByFloat", "null1", 10.5));
+ Assert.Equal(3.8m, rds.HIncrByFloat("TestHIncrByFloat", "null1", 3.3m));
+ Assert.Equal(14.3m, rds.HIncrByFloat("TestHIncrByFloat", "null1", 10.5m));
}
[Fact]
diff --git a/test/CSRedisCore.Tests/CSRedisClientStringTests.cs b/test/CSRedisCore.Tests/CSRedisClientStringTests.cs
index bc73825..4e7de86 100644
--- a/test/CSRedisCore.Tests/CSRedisClientStringTests.cs
+++ b/test/CSRedisCore.Tests/CSRedisClientStringTests.cs
@@ -142,7 +142,7 @@ public void IncrBy() {
key = "TestIncrBy";
Assert.Equal(1, rds.IncrBy(key, 1));
Assert.Equal(11, rds.IncrBy(key, 10));
- Assert.Equal(21.5, rds.IncrByFloat(key, 10.5));
+ Assert.Equal(21.5m, rds.IncrByFloat(key, 10.5m));
}
[Fact]