我正在解析一个在线提要(tcp中继),每秒发送大约30-50条消息(300-500行数据).消息包含两种类型的信息:订单和历史.
所以,对于订单,每个订单都有一个唯一的ID,我有:
private static Dictionary<long,MarketOrder> MarketOrders = new Dictionary<long,MarketOrder>();
Run Code Online (Sandbox Code Playgroud)
在我们进入时插入订单.数据来自缓存文件,因此邮件可以包含旧的数据,必须过滤掉.我现在正在这样做:
if (MarketOrders.ContainsKey(order.OrderID))
{
// If record exists in a dictionary add hits and overwrite the object if newer.
int hits = MarketOrders[order.OrderID].Hits;
if (MarketOrders[order.OrderID].LastUpdated < order.LastUpdated)
{
MarketOrders[order.OrderID] = order;
}
MarketOrders[order.OrderID].Hits = hits + 1;
}
else
{
// If not, add new one
order.Hits = 1;
MarketOrders.Add(order.OrderID, order);
}
Run Code Online (Sandbox Code Playgroud)
这在BackgroundWorker进程中运行,当字典项计数达到2500时,它被深度克隆(使用二进制序列化程序),清除并启动另一个后台进程,将克隆的副本插入数据库.清除字典后,将再次插入订单.所以基本上我试图尽可能多地接收并批量插入数据库.
我正在尝试用历史数据做类似的事情.没有唯一的ID,唯一性来自<int, int, DateTime>价值组合.
我需要一种从这三个值生成唯一键的快速方法,因此我可以将它存储在字典中,就像我对订单一样,或者存储和过滤该数据的另一种方法.
有什么建议?我的目标是.NET 4.0.
您可以创建Guid并使用这是关键:
byte[] bytes = new byte[16];
BitConverter.GetBytes(i1).CopyTo(bytes, 0);
BitConverter.GetBytes(i2).CopyTo(bytes, 4);
BitConverter.GetBytes(dt.Ticks).CopyTo(bytes, 8);
Guid key = new Guid(bytes);
Run Code Online (Sandbox Code Playgroud)
Dictionary<Guid, int>使用 a与 a循环运行上面的代码Dictionary<Tuple<int, int, DateTime>, int>,Guid关键似乎更快,但您应该在您的场景中测试它。
只是为了澄清一下,我使用了 aDictionary<Guid, int>来测试,但在你的情况下它将是一个Dictionary<Guid, YourHistoryType>. 如果在代码中发生其他所有事情时,使用 aGuid和 之间的任何差异Tuple<int, int, DateTime>可以忽略不计,并且您可以使用感觉更合适的那个,我不会感到惊讶。
| 归档时间: |
|
| 查看次数: |
1999 次 |
| 最近记录: |