A thread-safe collection type for a fixed length of elements, overwriting the oldest element
$ dotnet add package Soenneker.Utils.ConcurrentCircularQueue
Soenneker.Utils.ConcurrentCircularQueuedotnet add package Soenneker.Utils.ConcurrentCircularQueue
Instantiate a ConcurrentCircularQueue<T> object by specifying the maximum size of the queue. Optionally, asynchronous locking is available for perfect .Contains().
// Creates a queue with a maximum size of 3.
var myQueue = new ConcurrentCircularQueue<int>(3, locking: false);
Add an item to the queue. If the queue has reached its maximum size, the oldest item will be removed.
await myQueue.Enqueue(1);
await myQueue.Enqueue(2);
await myQueue.Enqueue(3);
await myQueue.Enqueue(4);
// The queue now contains 2, 3, and 4.Remove and return the oldest item from the queue.
(bool success, int result) = await myQueue.TryDequeue();Determine if a specific item is in the queue.
bool exists = await myQueue.Contains(item);Retrieve the current number of items in the queue.
int currentCount = await myQueue.Count();