TaskRefCache.cs 2.6 KB

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