/
FixedSizeQueue.cs
44 lines (38 loc) · 1.21 KB
/
FixedSizeQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
namespace Azure.Functions.Cli.Telemetry.PersistenceChannel
{
/// <summary>
/// A light fixed size queue. If Enqueue is called and queue's limit has reached the last item will be removed.
/// This data structure is thread safe.
/// </summary>
internal class FixedSizeQueue<T>
{
private readonly int _maxSize;
private readonly Queue<T> _queue = new Queue<T>();
private readonly object _queueLockObj = new object();
internal FixedSizeQueue(int maxSize)
{
_maxSize = maxSize;
}
internal void Enqueue(T item)
{
lock (_queueLockObj)
{
if (_queue.Count == _maxSize)
{
_queue.Dequeue();
}
_queue.Enqueue(item);
}
}
internal bool Contains(T item)
{
lock (_queueLockObj)
{
return _queue.Contains(item);
}
}
}
}