AVS-Carton-Shipment-Verifier/Services/InMemoryScanHistoryStore.cs

33 lines
776 B
C#

using System.Collections.Concurrent;
using AVSCartonShipmentVerifier.DTOs;
namespace AVSCartonShipmentVerifier.Services;
public sealed class InMemoryScanHistoryStore : IScanHistoryStore
{
private const int MaxItems = 5;
private readonly ConcurrentQueue<ScannedRecordDto> _records = new();
private readonly Lock _syncLock = new();
public void Add(ScannedRecordDto record)
{
lock (_syncLock)
{
_records.Enqueue(record);
while (_records.Count > MaxItems)
{
_records.TryDequeue(out _);
}
}
}
public IReadOnlyCollection<ScannedRecordDto> GetLastFive()
{
lock (_syncLock)
{
return _records.Reverse().ToArray();
}
}
}