123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using DW5S.DTO;
- using DW5S.Entity;
- namespace DW5S.App
- {
- /// <summary>
- /// 任务参考信号缓存
- /// </summary>
- public class TaskRefCache
- {
- public static TaskRefCache Instance { get; private set; } = new TaskRefCache();
- /// <summary>
- /// 缓存参考信号信息
- /// </summary>
- private Dictionary<int, List<TaskCgDto>> _cahceRefs = new Dictionary<int, List<TaskCgDto>>();
- public void AddRange(int taskId, List<TaskCgDto> cgItems)
- {
- lock (this)
- {
- if (!_cahceRefs.ContainsKey(taskId))
- _cahceRefs[taskId] = new List<TaskCgDto>();
- _cahceRefs[taskId].AddRange(cgItems);
- }
- }
- public void ClearCache(int taskID)
- {
- lock (this)
- {
- if (_cahceRefs.ContainsKey(taskID))
- {
- _cahceRefs.Remove(taskID);
- }
- }
- }
- /// <summary>
- /// 获取相同任务时间内的参考信号
- /// </summary>
- /// <param name="taskID"></param>
- /// <param name="start">开始时间</param>
- /// <param name="end">结束时间</param>
- /// <returns></returns>
- public List<TaskCgDto> GetTaskRef(int taskID, DateTime start, DateTime end)
- {
- lock (this)
- {
- if (_cahceRefs.ContainsKey(taskID))
- {
- var filterRefs = _cahceRefs[taskID].Where(r => r.SigTime >= start && r.SigTime <= end).ToList();
- return filterRefs;
- }
- else
- {
- return new List<TaskCgDto>();
- }
- }
- }
- /// <summary>
- /// 清理过期参考信号
- /// </summary>
- /// <param name="taskID"></param>
- /// <returns></returns>
- public void ClearExpireRef(int taskID)
- {
- lock (this)
- {
- if (_cahceRefs.ContainsKey(taskID))
- {
- var maxtime = _cahceRefs[taskID].Max(m => m.SigTime);
- var mintime = maxtime.AddHours(-1);//清理一个小时的参考信号
- _cahceRefs[taskID].RemoveAll(r => r.SigTime <= mintime);
- }
-
- }
- }
-
- public void ClearAll()
- {
- lock (this)
- {
- _cahceRefs.Clear();
- }
- }
- }
- }
|