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?
50000 Timers may be more of a problem than the Dictionary.
I would add them to a
List<> orQueue<> and use 1 Timer, set to your max tolerance (~ 30 seconds?) and every time the Timer elapses, batch remove all expired orders.