Cyclic buffer with readers
I need a开发者_如何学C cyclic buffer implementation that supports seekable readers. My use case:
In my code, I collect log messages. Eventually, the user might visit a page which shows them nicely formatted. To make sure the messages don't fill the RAM, I need a fixed size FIFO structure. If the user doesn't visit the page for a long time, messages get dropped. That's OK.
As long as the user stays on the page, new log messages should be appended to the page. Via JavaScript, the user can define how many messages to keep. This is completely independent of the buffer size in my app. So I need a reader on the data structure which I can use to iterate over any new elements.
If the user reloads the page or loads it for the first time, I need to set the reader to the oldest element in the FIFO.
As messages are added, the reader must be updated. If the browser fails to fetch the new messages fast enough, the reader should eventually point to the oldest message in the FIFO. That means the user can miss a couple of messages. That's not perfect but it should be an uncommon case. If the reader could tell me "missed 5 messages", that would be perfect, but I can live without that.
Do you know an existing implementation which offers this?
Try http://commons.apache.org/collections/, look at the circular buffers there.
精彩评论