Cyclic buffer with readers

时光总嘲笑我的痴心妄想 提交于 2019-12-13 16:47:15

问题


I need a 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?


回答1:


Try http://commons.apache.org/collections/, look at the circular buffers there.



来源:https://stackoverflow.com/questions/6135570/cyclic-buffer-with-readers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!