using System.Collections.Concurrent; namespace AutoAgent.Domain.Models { public class ConcurrentObject { internal readonly object _lock = new object(); public void LockedSet(ref T dst, T value) { lock (_lock) dst = value; } public T LockedGet(ref T src) { lock (_lock) return src; } } public class LazyConcurrentContainer : ConcurrentObject { private DateTime _lastScan = DateTime.MinValue; private DateTime _currentScan = DateTime.MinValue; private ConcurrentDictionary? _container; public DateTime LastUpdate { get => LockedGet(ref _lastScan); } public TimeSpan Elapsed { get { lock (_lock) return _currentScan - _lastScan; } } public ConcurrentDictionary? Container { get => LockedGet(ref _container); set { lock (_lock) { _lastScan = _currentScan; _currentScan = DateTime.UtcNow; _container = value; } } } public IEnumerable? Values { get => _container?.Values.ToList(); } public void SetContainer(Dictionary? container) { Container = container != null ? new ConcurrentDictionary(container) : null; } public void SetContainer(ConcurrentDictionary container) { Container = container != null ? new ConcurrentDictionary(container) : null; } public void CleanContainer() { Container = null; } } }