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

35 lines
976 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 Task AddAsync(ScannedRecordDto record, CancellationToken cancellationToken = default)
{
lock (_syncLock)
{
_records.Enqueue(record);
while (_records.Count > MaxItems)
{
_records.TryDequeue(out _);
}
}
return Task.CompletedTask;
}
public Task<IReadOnlyCollection<ScannedRecordDto>> GetLastFiveAsync(CancellationToken cancellationToken = default)
{
lock (_syncLock)
{
return Task.FromResult<IReadOnlyCollection<ScannedRecordDto>>(_records.Reverse().ToArray());
}
}
}