2881099 / FreeRedis

🦄 FreeRedis is .NET40+ redis client. supports cluster, sentinel, master-slave, pub-sub, lua, pipeline, transaction, streams, client-side-caching, and pooling.
MIT License
920 stars 164 forks source link

此处代码有非必要操作 #187

Open lixiaoqiang opened 4 months ago

lixiaoqiang commented 4 months ago

在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 4 months ago

因为不确定是否会被外部引用修改,所以这样写是最安全。