此处代码有非必要操作
lixiaoqiang opened this issue · 1 comments
lixiaoqiang commented
在ClusterAdapter的构造函数中,我认为可以将如下代码
public ClusterAdapter(RedisClient topOwner, ConnectionStringBuilder[] clusterConnectionStrings, Dictionary<string, string> hostMappings)
{
UseType = UseType.Cluster;
TopOwner = topOwner;
if (clusterConnectionStrings.Any() != true)
throw new ArgumentNullException(nameof(clusterConnectionStrings));
_clusterConnectionStrings = clusterConnectionStrings.ToArray();
// 这里进行了循环Copy
if (hostMappings != null)
{
// 这里构造了一个新的字典实例
_hostMappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var kv in hostMappings)
_hostMappings.Add(kv.Key, kv.Value);
}
// Copy完后又对Copy过程中生成了新的字典实例进行丢弃。
_hostMappings = hostMappings;
_baseEncoding = _clusterConnectionStrings.FirstOrDefault()?.Encoding;
_ib = new IdleBus<RedisClientPool>(TimeSpan.FromMinutes(10));
RefershClusterNodes();
}
更改为
public ClusterAdapter(RedisClient topOwner, ConnectionStringBuilder[] clusterConnectionStrings, Dictionary<string, string> hostMappings)
{
UseType = UseType.Cluster;
TopOwner = topOwner;
if (clusterConnectionStrings.Any() != true)
throw new ArgumentNullException(nameof(clusterConnectionStrings));
_clusterConnectionStrings = clusterConnectionStrings.ToArray();
_hostMappings = hostMappings;
_baseEncoding = _clusterConnectionStrings.FirstOrDefault()?.Encoding;
_ib = new IdleBus<RedisClientPool>(TimeSpan.FromMinutes(10));
RefershClusterNodes();
}
2881099 commented
因为不确定是否会被外部引用修改,所以这样写是最安全。