using System.IO; using CYQ.Data.Tool; namespace CYQ.Data.Cache { /// /// 用于监控分布式缓存的配置列表,实际高可用,动态加载,而不需要关停运行的服务。 /// internal class HostConfigWatch { //用于验证权限的委托事件。 internal delegate void OnConfigChangedDelegate(string configValue); public event OnConfigChangedDelegate OnConfigChangedEvent; public HostConfigWatch(string configValue) { StartWatch(configValue); CompareHostListByConfig(configValue); } private void StartWatch(string configValue) { if (string.IsNullOrEmpty(configValue)) { return; } if (configValue.EndsWith(".txt") || configValue.EndsWith(".ini")) { string path = AppConfig.RunPath + configValue; if (File.Exists(path)) { IOWatch.On(path, delegate (FileSystemEventArgs e) { if (OnConfigChangedEvent != null) { OnConfigChangedEvent(IOHelper.ReadAllText(path)); } }); } } } /// /// 存档配置文件对应的原始主机列表。 /// public MList HostList = new MList(); /// /// 变化后要追加的主机。 /// public MList HostAddList = new MList(); /// /// 变化后要移除的主机。 /// public MList HostRemoveList = new MList(); /// /// 和旧配置相比较,找出变化后的主机信息 /// /// 新的配置值 public void CompareHostListByConfig(string configValue) { lock (this) { if (string.IsNullOrEmpty(configValue)) { return; } string[] hostItems = configValue.Trim().Split(','); if (hostItems == null || hostItems.Length == 0) { return; } HostAddList.Clear(); HostRemoveList.Clear(); //开始比较主机配置版本差异 if (HostList.Count == 0) { foreach (string host in hostItems) { string item = host.Trim(); if (string.IsNullOrEmpty(item) || item.StartsWith("#")) { continue; } if (!HostAddList.Contains(item)) { HostAddList.Add(item); HostList.Add(item); } } } else { foreach (string host in hostItems) { string item = host.Trim(); if (string.IsNullOrEmpty(item) || item.StartsWith("#")) { continue; } if (HostList.Contains(item)) { HostList.Remove(item); } else if (!HostAddList.Contains(item)) { HostAddList.Add(item); } } //剩下的就是要移除的。 foreach (string host in HostList.List) { HostRemoveList.Add(host); } HostList.Clear(); //把项添加到HostList项。 foreach (string host in hostItems) { string item = host.Trim(); if (string.IsNullOrEmpty(item) || item.StartsWith("#")) { continue; } if (!HostList.Contains(item)) { HostList.Add(item); } } } } } } }