using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace TrainInfomationProviderService.TrainInfo.Entities { public class IndexStorage { private readonly object _lockObject = new object(); /// /// 创建 的新实例(IndexStorage) /// public IndexStorage() { DateIndices = new HashSet(StringComparer.OrdinalIgnoreCase); TrainInfoStorages = new Dictionary(); } public int Version { get; set; } public HashSet DateIndices { get; set; } [JsonIgnore] public Dictionary TrainInfoStorages { get; set; } [OnDeserialized] void Init(StreamingContext ctx) { lock (_lockObject) { var dataLoader = new WebDataProvider(); TrainInfoStorages.Clear(); var baseDate = DateTime.Now.Date; DateIndices = DateIndices.Where(s => s.ToDateTime() >= baseDate).ToHashSet(StringComparer.OrdinalIgnoreCase); foreach (var dateIndex in DateIndices.ToArray()) { var data = dataLoader.LoadCache(dateIndex); if (data == null) DateIndices.Remove(dateIndex); else { data.InitStationTrainData(); TrainInfoStorages.Add(dateIndex, data); } } } } /// /// 移除过时的信息 /// internal void RemoveOutdateStorage() { lock (_lockObject) { var baseDate = DateTime.Now.Date; var outdates = DateIndices.Where(s => s.ToDateTime() < baseDate).ToArray(); Trace.TraceInformation("[TRAIN_INDEX_STORAGE] 正在检查过期数据"); foreach (var outdate in outdates) { Trace.TraceInformation("[TRAIN_INDEX_STORAGE] 正在移除过期数据 {0}", outdate); DateIndices.Remove(outdate); if (TrainInfoStorages.ContainsKey(outdate)) TrainInfoStorages.Remove(outdate); Trace.TraceInformation("[TRAIN_INDEX_STORAGE] 完成移除过期数据 {0}", outdate); } Trace.TraceInformation("[TRAIN_INDEX_STORAGE] 正在移除过期数据"); } } } }