ZhiYeJianKang_PeiXun/cyqdata-master/Cache/MemRedis/HostConfigWatch.cs

148 lines
5.1 KiB
C#
Raw Normal View History

2025-02-20 15:41:53 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using CYQ.Data.Tool;
namespace CYQ.Data.Cache
{
/// <summary>
/// <20><><EFBFBD>ڼ<EFBFBD><DABC>طֲ<D8B7>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD>ʵ<EFBFBD>ʸ߿<CAB8><DFBF>ã<EFBFBD><C3A3><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ͣ<EFBFBD><CDA3><EFBFBD>еķ<D0B5><C4B7><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
internal class HostConfigWatch
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤Ȩ<D6A4>޵<EFBFBD>ί<EFBFBD><CEAF><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
internal delegate void OnConfigChangedDelegate();
public event OnConfigChangedDelegate OnConfigChangedEvent;
CacheType cacheType;
string configValue;
public HostConfigWatch(CacheType cacheType, string configValue)
{
this.cacheType = cacheType;
this.configValue = configValue;
ResetHostListByConfig(true);
}
/// <summary>
/// <20><EFBFBD><E6B5B5><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Ӧ<EFBFBD><D3A6>ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD>
/// </summary>
public MList<string> HostList = new MList<string>();
/// <summary>
/// <20><EFBFBD><E4BBAF>Ҫ׷<D2AA>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public MList<string> HostAddList = new MList<string>();
/// <summary>
/// <20><EFBFBD><E4BBAF>Ҫ<EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public MList<string> HostRemoveList = new MList<string>();
//private void StartFileSystemWatcher(string fileName)
//{
// FileSystemWatcher fsy = new FileSystemWatcher(Path.GetDirectoryName(fileName), Path.GetFileName(fileName));
// fsy.EnableRaisingEvents = true;
// fsy.IncludeSubdirectories = false;
// fsy.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
// fsy.Changed += new FileSystemEventHandler(fsy_Changed);
//}
//
//private void fsy_Changed(object sender, FileSystemEventArgs e)
//{
//}
private static readonly object obj2 = new object();
private void ResetHostListByConfig(bool isNeedStartWatch)
{
lock (obj2)
{
if (string.IsNullOrEmpty(configValue)) { return; }
string[] hostItems = null;
if (configValue.EndsWith(".txt") || configValue.EndsWith(".ini"))
{
string path = AppConfig.RunPath + configValue;
if (File.Exists(path))
{
if (isNeedStartWatch)
{
IOWatch.On(path, delegate (FileSystemEventArgs e) {
ResetHostListByConfig(false);
if (OnConfigChangedEvent != null)
{
OnConfigChangedEvent();
}
});
}
hostItems = IOHelper.ReadAllLines(path);
}
}
else
{
hostItems = configValue.Trim().Split(',');
}
if (hostItems == null || hostItems.Length == 0) { return; }
HostAddList.Clear();
HostRemoveList.Clear();
//<2F><>ʼ<EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ð汾<C3B0><E6B1BE><EFBFBD><EFBFBD>
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);
}
}
//ʣ<>µľ<C2B5><C4BE><EFBFBD>Ҫ<EFBFBD>Ƴ<EFBFBD><C6B3>ġ<EFBFBD>
foreach (string host in HostList.List)
{
HostRemoveList.Add(host);
}
HostList.Clear();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>HostList<73>
foreach (string host in hostItems)
{
string item = host.Trim();
if (string.IsNullOrEmpty(item) || item.StartsWith("#"))
{
continue;
}
if (!HostList.Contains(item))
{
HostList.Add(item);
}
}
}
}
}
}
}