TaskRefCache.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using DW5S.DTO;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. namespace DW5S.Service
  9. {
  10. /// <summary>
  11. /// 任务参考信号缓存
  12. /// </summary>
  13. public class TaskRefCache
  14. {
  15. public static TaskRefCache Instance { get; private set; } = new TaskRefCache();
  16. /// <summary>
  17. /// 缓存参考信号信息
  18. /// </summary>
  19. private Dictionary<int, List<TaskCgDto>> _cahceRefs = new Dictionary<int, List<TaskCgDto>>();
  20. public void AddRange(int taskId, List<TaskCgDto> cgItems)
  21. {
  22. lock (this)
  23. {
  24. if (!_cahceRefs.ContainsKey(taskId))
  25. _cahceRefs[taskId] = new List<TaskCgDto>();
  26. _cahceRefs[taskId].AddRange(cgItems);
  27. }
  28. }
  29. public void ClearCache(int taskID)
  30. {
  31. lock (this)
  32. {
  33. if (_cahceRefs.ContainsKey(taskID))
  34. {
  35. _cahceRefs.Remove(taskID);
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// 获取相同任务时间内的参考信号
  41. /// </summary>
  42. /// <param name="taskID"></param>
  43. /// <param name="start">开始时间</param>
  44. /// <param name="end">结束时间</param>
  45. /// <returns></returns>
  46. public List<TaskCgDto> GetTaskRef(int taskID, DateTime start, DateTime end)
  47. {
  48. lock (this)
  49. {
  50. if (_cahceRefs.ContainsKey(taskID))
  51. {
  52. var filterRefs = _cahceRefs[taskID].Where(r => r.SigTime >= start && r.SigTime <= end).ToList();
  53. return filterRefs;
  54. }
  55. else
  56. {
  57. return new List<TaskCgDto>();
  58. }
  59. }
  60. }
  61. /// <summary>
  62. /// 清理过期参考信号
  63. /// </summary>
  64. /// <param name="taskID"></param>
  65. /// <returns></returns>
  66. public void ClearExpireRef(int taskID)
  67. {
  68. lock (this)
  69. {
  70. if (_cahceRefs.ContainsKey(taskID))
  71. {
  72. var maxtime = _cahceRefs[taskID].Max(m => m.SigTime);
  73. var mintime = maxtime.AddHours(-1);//清理一个小时的参考信号
  74. _cahceRefs[taskID].RemoveAll(r => r.SigTime <= mintime);
  75. }
  76. }
  77. }
  78. public void ClearAll()
  79. {
  80. lock (this)
  81. {
  82. _cahceRefs.Clear();
  83. }
  84. }
  85. }
  86. }