如何保存与 OrderId 连接的计时器?

发布于 2024-09-04 02:37:15 字数 669 浏览 3 评论 0原文

我有一个客户可以下订单的系统。下单后,他们有 60 分钟的时间付款,然后订单就会被删除。在服务器端下订单时,我创建计时器并将运行时间设置为 60 分钟

System.Timer.Timers timer = new System.Timers.Timer(1000*60*60);
timer.AutoReset = false;
timer.Elapsed += HandleElapsed;
timer.Start();

因为我希望能够在客户决定付款时处理计时器,并且如果他不付款我希望能够取消订单我保留两个字典:

Dictionary<int, Timer> _orderTimer;
Dictionary<Timer, int> _timerOrder;

然后,当客户付款时,由于 _orderTimer 字典,我可以通过 orderId 以 O(1) 访问计时器,并且当时间流逝时,由于 _timerOrder 字典,我可以以 O(1) 访问订单。

我的问题是:这是个好方法吗?假设一瞬间我必须在字典中保存的最大行数为 50000 ?

也许最好从 Timer 类派生,添加名为 OrderId 的属性,将其全部保留在 List 中并使用 linq 搜索订单/计时器?

或者也许你应该以不同的方式来做这件事?

I have a system where clients can make orders. After making order they have 60 minutes to pay fot it before it will be deleted. On the server side when order is made i create timer and set elapsed time to 60 minutes

System.Timer.Timers timer = new System.Timers.Timer(1000*60*60);
timer.AutoReset = false;
timer.Elapsed += HandleElapsed;
timer.Start();

Because I want to be able to dispose timer if client decides to pay and I want to be able to cancel order if he doesn't I keep two Dictionaries:

Dictionary<int, Timer> _orderTimer;
Dictionary<Timer, int> _timerOrder;

Then when client pay's I can access Timer by orderId with O(1) thanks to _orderTimer dictionary and when time elapsed I can access order with O(1) thanks to _timerOrder dictionary.

My question is: Is it good aproach? Assuming that max number of rows I have to keep in dictionary in one moment will be 50000?

Maybe it would be better to derive from Timer class, add property called OrderId, keep it all in List and search for order/timer using linq?

Or maybe you I should do this in different way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

留一抹残留的笑 2024-09-11 02:37:15

50000 个计时器可能比字典更成问题。

我会将它们添加到 List<> 中或队列<>并使用 1 个计时器,设置为最大容差(~ 30 秒?),每次计时器到期时,批量删除所有过期订单。

50000 Timers may be more of a problem than the Dictionary.

I would add them to a List<> or Queue<> and use 1 Timer, set to your max tolerance (~ 30 seconds?) and every time the Timer elapses, batch remove all expired orders.

迟月 2024-09-11 02:37:15

正如对 Henks 答案的添加一样,我将创建一个按过期日期排序的 SortedList 。因此,您的单个计时器线程只需枚举列表,直到日期/时间超出范围。

Just as an add to Henks answer i would make a SortedList<Order> which is sorted by the expired date. So your single timer thread just has to enumerate through the list till a date/time is out of scope.

属性 2024-09-11 02:37:15

我不知道这是否可行,但另一种方法是将其过期时间存储在数据存储中,然后编写一个计时器,然后拾取所有适当的记录并决定如何处理它们。

内存中有 50,000 多个计时器似乎有点……可怕。

I don't know if this is viable, but another approach would be to store the time it expires in your data store, then just write one timer that then picks up all the appropriate records and decides what to do with them.

Having 50,000+ timers sitting out there in memory just seems a little...scary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文