/// <summary> /// 任务队列接口 /// </summary> public interface ITaskQueue<T> { /// <summary> /// 增加一个对象 /// </summary> /// <param name="t"></param> void Add(T t); /// <summary> /// 获取一个分组队列 /// </summary> /// <returns></returns> IList<T> GetQueue(); /// <summary> /// 是否阻塞增加 /// </summary> /// <returns></returns> bool IsWaitAdd(); /// <summary> /// 当前队列完成 /// </summary> void Complete(); } /// <summary> /// 任务队列 /// </summary> public class TaskQueue<T> : ITaskQueue<T>, IDisposable { /// <summary> /// 内置队列 /// </summary> private ConcurrentDictionary<int, IList<T>> taskQueues; /// <summary> /// 分区大小 /// </summary> private int PartitionSize; /// <summary> /// 默认index为0 /// </summary> private int Index = 0; /// <summary> /// 默认处理偏移 /// </summary> private int OffSet = 0; /// <summary> /// 内置锁 /// </summary> private object Lock = new object(); /// <summary> /// <summary> /// 构造 /// </summary> /// <param name="PartitionSize">分区大小,默认分区大小为10 </param> public TaskQueue(int PartitionSize = 10) { taskQueues = new ConcurrentDictionary<int, IList<T>>(); this.PartitionSize = PartitionSize; List<T> ts = new List<T>(); taskQueues.AddOrUpdate(Index, ts, (k, v) => ts); } /// <summary> /// 增加一个对象 /// </summary> /// <param name="t"></param> public void Add(T t) { lock (Lock) { IList<T> ts; if (taskQueues.TryGetValue(Index, out ts)) { if (ts.Count < PartitionSize) { ts.Add(t); taskQueues.AddOrUpdate(Index, ts, (k, v) => ts); } else //超出区域范围,则新建区 { Index++; IList<T> ts1 = new List<T>(); ts1.Add(t); taskQueues.AddOrUpdate(Index, ts1, (k, v) => ts1); } } else { IList<T> ts1 = new List<T>(); ts1.Add(t); taskQueues.AddOrUpdate(Index, ts1, (k, v) => ts1); } } } /// <summary> /// 获取一个分组队列 /// </summary> public IList<T> GetQueue() { lock (Lock) { IList<T> ts; if (taskQueues.TryGetValue(OffSet, out ts)) { if (OffSet == Index)//如果直接获取一个能用的,那就新建区为新区 { Index++; } return ts; } return null; } } /// <summary> /// 是否阻塞增加 /// </summary> public bool IsWaitAdd() { lock (Lock) { if (OffSet != Index) { return true; } return false; } } /// <summary> /// 当前队列完成 /// </summary> public void Complete() { lock (Lock) { IList<T> ts; taskQueues.TryRemove(OffSet, out ts); if (OffSet < Index) { OffSet++; } } } public void Dispose() { if (taskQueues != null) { taskQueues.Clear(); taskQueues = null; } } }
/// <summary> /// List分页处理 /// </summary> public class ListPage<T> { /// <summary> /// 页数 /// </summary> public int PageCount { get; set; } /// <summary> /// 当前页码 /// </summary> public int CurrPage { get; set; } /// <summary> /// 一页多少条 /// </summary> public int PageSize { get; set; } /// <summary> /// 数据源信息 /// </summary> private List<T> DataSource { get; set; } /// <summary> /// 获取数据源和一页多少 /// </summary> /// <param name="List"></param> /// <param name="PageSize"></param> public ListPage(List<T> List, int PageSize = 4, int CurrPage = 0) { DataSource = List; this.PageSize = PageSize; this.PageCount = (int)Math.Ceiling((decimal)DataSource.Count / PageSize); this.CurrPage = CurrPage; } //是否有下一页 public bool HasNextPage { get { return CurrPage < this.PageCount; } } /// <summary> /// 直接获取下一页 /// </summary> /// <returns></returns> public List<T> NextPage() { var data = DataSource.Skip((CurrPage) * PageSize).Take(PageSize).ToList(); CurrPage++; return data; } /// <summary> /// 获取指定页 /// </summary> /// <returns></returns> public List<T> getPage(int CurrPage) { var data = DataSource.Skip((CurrPage) * PageSize).Take(PageSize).ToList(); return data; } }
/// <summary> /// 配置信息(用来读取相应配置文件) /// </summary> public class ConfigInfo<T> where T : class, new() { /// <summary> /// 配置地址 /// </summary> public string FileName { get; set; } /// <summary> /// 文件名 /// </summary> public string FilePath { get; set; } /// <summary> /// 当前配置 /// </summary> public T CurrentConfig { get; set; } /// <summary> /// 读写锁 /// </summary> private ReaderWriterLockSlim rwl = new ReaderWriterLockSlim(); /// <summary> /// 构造函数 /// </summary> public ConfigInfo(string FileName) { this.FileName = FileName; this.FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FileName); CurrentConfig = Read(); } /// <summary> /// 读取配置信息 /// </summary> /// <returns></returns> public T Read() { T t = new T(); if (File.Exists(FilePath)) { try { rwl.EnterReadLock(); var Str = File.ReadAllText(FilePath); if (!string.IsNullOrEmpty(Str)) { try { t = JsonConvert.DeserializeObject<T>(Str); CurrentConfig = t; return t; } catch (Exception) { } } } finally { rwl.ExitReadLock(); } } else { Save(t); } return t; } /// <summary> /// 写配置 /// </summary> /// <param name="t"></param> public void Save(T t) { try { rwl.EnterWriteLock(); if (!File.Exists(FilePath)) { try { Directory.CreateDirectory(new FileInfo(FilePath).DirectoryName); CurrentConfig = t; } catch (Exception) { } } File.WriteAllText(FilePath, JsonConvert.SerializeObject(t)); } catch (Exception) { } finally { rwl.ExitWriteLock(); } } /// <summary> /// 写配置 /// </summary> /// <param name="t"></param> public void Save() { try { rwl.EnterWriteLock(); if (!File.Exists(FilePath)) { try { Directory.CreateDirectory(new FileInfo(FilePath).DirectoryName); } catch (Exception) { } } File.WriteAllText(FilePath, JsonConvert.SerializeObject(CurrentConfig)); } catch (Exception) { } finally { rwl.ExitWriteLock(); } } }
标签:Index,配置文件,int,List,ts,taskQueues,NET,public From: https://www.cnblogs.com/YSAN718596403/p/16664759.html