2014-11-21 20:32:36 +08:00
|
|
|
|
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
|
|
|
|
|
{
|
2014-11-27 23:25:36 +08:00
|
|
|
|
private readonly object _lockObject = new object();
|
|
|
|
|
|
2014-11-21 20:32:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建 <see cref="IndexStorage" /> 的新实例(IndexStorage)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IndexStorage()
|
|
|
|
|
{
|
|
|
|
|
DateIndices = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
TrainInfoStorages = new Dictionary<string, TrainInfoStorage>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Version { get; set; }
|
|
|
|
|
|
|
|
|
|
public HashSet<string> DateIndices { get; set; }
|
|
|
|
|
|
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public Dictionary<string, TrainInfoStorage> TrainInfoStorages { get; set; }
|
|
|
|
|
|
|
|
|
|
[OnDeserialized]
|
|
|
|
|
void Init(StreamingContext ctx)
|
|
|
|
|
{
|
2014-11-27 23:25:36 +08:00
|
|
|
|
lock (_lockObject)
|
2014-11-21 20:32:36 +08:00
|
|
|
|
{
|
2014-11-27 23:25:36 +08:00
|
|
|
|
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 TrainInfoStorages.Add(dateIndex, data);
|
|
|
|
|
}
|
2014-11-21 20:32:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 移除过时的信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal void RemoveOutdateStorage()
|
|
|
|
|
{
|
2014-11-27 23:25:36 +08:00
|
|
|
|
lock (_lockObject)
|
2014-11-21 20:32:36 +08:00
|
|
|
|
{
|
2014-11-27 23:25:36 +08:00
|
|
|
|
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] 正在移除过期数据");
|
2014-11-21 20:32:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-27 23:25:36 +08:00
|
|
|
|
|
2014-11-21 20:32:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|