Skip to content

Commit

Permalink
v1.2.14 - add Cluster hostMappings parameter;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Mar 15, 2024
1 parent 51c63a0 commit 22492c3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/FreeRedis/FreeRedis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>FreeRedis</AssemblyName>
<PackageId>FreeRedis</PackageId>
<RootNamespace>FreeRedis</RootNamespace>
<Version>1.2.13</Version>
<Version>1.2.14</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/2881099/FreeRedis</PackageProjectUrl>
<Description>FreeRedis is .NET redis client, supports cluster, sentinel, master-slave, pipeline, transaction and connection pool.</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/FreeRedis/FreeRedis.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/FreeRedis/RedisClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public RedisClient(ConnectionStringBuilder connectionString, params ConnectionSt
/// <summary>
/// Cluster RedisClient
/// </summary>
public RedisClient(ConnectionStringBuilder[] clusterConnectionStrings)
public RedisClient(ConnectionStringBuilder[] clusterConnectionStrings, Dictionary<string, string> hostMappings = null)
{
Adapter = new ClusterAdapter(this, clusterConnectionStrings);
Adapter = new ClusterAdapter(this, clusterConnectionStrings, hostMappings);
Prefix = clusterConnectionStrings[0].Prefix;
ConnectionString = clusterConnectionStrings[0];
}
Expand Down
12 changes: 11 additions & 1 deletion src/FreeRedis/RedisClient/Adapter/ClusterAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ internal class ClusterAdapter : BaseAdapter
{
internal readonly IdleBus<RedisClientPool> _ib;
internal readonly ConnectionStringBuilder[] _clusterConnectionStrings;
internal readonly Dictionary<string, string> _hostMappings;
internal static Encoding _baseEncoding;

public ClusterAdapter(RedisClient topOwner, ConnectionStringBuilder[] clusterConnectionStrings)
public ClusterAdapter(RedisClient topOwner, ConnectionStringBuilder[] clusterConnectionStrings, Dictionary<string, string> hostMappings)
{
UseType = UseType.Cluster;
TopOwner = topOwner;
Expand All @@ -26,6 +27,13 @@ public ClusterAdapter(RedisClient topOwner, ConnectionStringBuilder[] clusterCon
throw new ArgumentNullException(nameof(clusterConnectionStrings));

_clusterConnectionStrings = clusterConnectionStrings.ToArray();
if (hostMappings != null)
{
_hostMappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var kv in hostMappings)
_hostMappings.Add(kv.Key, kv.Value);
}
_hostMappings = hostMappings;
_baseEncoding = _clusterConnectionStrings.FirstOrDefault()?.Encoding;
_ib = new IdleBus<RedisClientPool>(TimeSpan.FromMinutes(10));
RefershClusterNodes();
Expand Down Expand Up @@ -304,6 +312,8 @@ void RefershClusterNodes()
endpoint = $"{DefaultRedisSocket.SplitHost(testConnection.Host).Key}:{endpoint.Substring(10)}";
else if (endpoint.StartsWith("localhost", StringComparison.CurrentCultureIgnoreCase))
endpoint = $"{DefaultRedisSocket.SplitHost(testConnection.Host).Key}:{endpoint.Substring(10)}";
else if (_hostMappings?.TryGetValue(endpoint, out var endpointMapping) == true)
endpoint = endpointMapping;
ConnectionStringBuilder connectionString = testConnection.ToString();
connectionString.Host = endpoint;
connectionString.CertificateValidation = testConnection.CertificateValidation;
Expand Down

0 comments on commit 22492c3

Please sign in to comment.