1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using DW5S.DTO;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace DW5S.Service
- {
- /// <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();
- }
- }
- }
- }
|