Circular Buffers

A Circular Buffer is a fixed-sized data structure that a sender can add messages to. As the sender adds more messages when the buffer is full, the oldest items will be replaced with new items. A receiver can attach itself to the buffer to read latest messages at any time.

Basics

To allocate a cache-based circular buffer that holds 5 most recent messages, use:
CachedCircularBuffer<int> buffer = new CachedCircularBuffer<int>("my_buffer", 5);

To add a new item to the buffer:
buffer.Add(1234);

And to get the newest item in the buffer:
int value = buffer[0];

Remarks

  • You can also trace back to older items by using negative indexes. For example, to get the 3rd newest item in the buffer, use:
int value = buffer[-2];
  • There are CompressedCircularBuffer types that support content compression and batched send. Messages will be cached locally and sent in batches of specified size. For example, to use a cache-based, gzip compressed, batched circular buffer, use:
CachedCompressedCircularBuffer buffer = new CachedCompressedCircularBuffer("perf_compressed", 1, batchSize: 5);
  • You can update the BatchSize attribute on a CompressedCircularBuffer at any time to adapt to network conditions.

Last edited Nov 4, 2013 at 4:08 AM by Haishi, version 2