简体   繁体   English

缓存和保存经常更新的数据

[英]Caching and saving frequently updated data

I'm working on a .NET data logging application that needs to accept data from a large number of clients and store it in a database. 我正在开发一个.NET数据记录应用程序,该应用程序需要接受来自大量客户端的数据并将其存储在数据库中。

The client sends a start event to the server and then sends heartbeat events, causing the last activity time of the data to be updated. 客户端将开始事件发送到服务器,然后发送心跳事件,从而导致数据的最后活动时间被更新。 I can't use an end event because the client app may be closed with no chance to send such an event. 我无法使用结束事件,因为客户端应用程序可能已关闭,没有机会发送此类事件。

A simple approach would be to do a db insert on the start event and then a db update on each heartbeat, but that would be very db-intensive with a heartbeat every few seconds from each of a large number of clients. 一种简单的方法是在start事件上进行数据库插入,然后在每个心跳上进行数据库更新,但是这将非常耗费数据库,每隔数秒就会从大量客户端中获取心跳。 The updates would also become expensive as the database table gets large. 随着数据库表变大,更新也将变得昂贵。

Thus, I am looking at caching the data in memory and then flushing it to the database when a client has stopped sending heartbeats. 因此,我正在查看将数据缓存在内存中,然后在客户端停止发送心跳时将其刷新到数据库。

So I need a suitable data structure and strategy for: 因此,我需要适合以下情况的数据结构和策略:

  • Creating a session object when a client sends a start event 客户端发送开始事件时创建会话对象
  • Efficiently updating the objects when heartbeat events are received 收到心跳事件时有效地更新对象
  • Identifying sessions that have timed out and saving them to the database 识别超时的会话并将其保存到数据库

I am thinking something along the lines of a hashtable in memory that is periodically iterated by a timer triggered event to check for timed out sessions. 我在想一些类似于哈希表中的内存的事情,该哈希表由计时器触发的事件定期进行迭代,以检查超时的会话。

Does that make sense or is there a better approach to this kind of problem? 这有意义还是有更好的方法来解决此类问题?

Depending on the nature of the logs, standard approach that is supported (as most logging application need to support a scenario where keeping logs is very important or required by law) is that logs are written to hard disk as fast as possible (bonus points for independent system which is write only from the perspective of the application environment that is logged). 根据日志的性质,受支持的标准方法(因为大多数日志记录应用程序都需要支持其中日志非常重要或法律要求保留的方案),因此应尽快将日志写入硬盘(这样做的好处)。独立系统,仅从记录的应用程序环境的角度进行写操作)。

Logs are processed, aggregated or analysed with lower priority. 日志以较低的优先级进行处理,汇总或分析。

Of course your use case can be different. 当然,您的用例可以有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM