Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lixiaoqiang opened this issue May 28, 2024 · 1 comment
Open

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

lixiaoqiang opened this issue May 28, 2024 · 1 comment

Comments

@lixiaoqiang
Copy link

lixiaoqiang commented May 28, 2024

在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
Copy link
Owner

2881099 commented May 28, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants