57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
/* This software is licensed by the MIT License, see LICENSE file */
|
|
/* Copyright © 2024-2025 Gregory Lirent */
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace WebmrAPI.Utils
|
|
{
|
|
public class LazyConcurrentContainer<T> : ConcurrentObject
|
|
{
|
|
private DateTime _lastScan = DateTime.MinValue;
|
|
private DateTime _currentScan = DateTime.MinValue;
|
|
private ConcurrentDictionary<long, T>? _container;
|
|
|
|
public DateTime LastUpdate
|
|
{
|
|
get => LockedGet(ref _lastScan);
|
|
}
|
|
|
|
public TimeSpan Elapsed
|
|
{
|
|
get { lock (_lock) return _currentScan - _lastScan; }
|
|
}
|
|
|
|
public ConcurrentDictionary<long, T>? Container
|
|
{
|
|
get => LockedGet(ref _container);
|
|
set
|
|
{
|
|
lock (_lock)
|
|
{
|
|
_lastScan = _currentScan;
|
|
_currentScan = DateTime.UtcNow;
|
|
_container = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public IEnumerable<T>? Values
|
|
{
|
|
get => _container?.Values.ToList();
|
|
}
|
|
|
|
public void SetContainer(Dictionary<long, T>? container)
|
|
{
|
|
Container = container != null ? new ConcurrentDictionary<long, T>(container) : null;
|
|
}
|
|
public void SetContainer(ConcurrentDictionary<long, T> container)
|
|
{
|
|
Container = container != null ? new ConcurrentDictionary<long, T>(container) : null;
|
|
}
|
|
public void CleanContainer()
|
|
{
|
|
Container = null;
|
|
}
|
|
}
|
|
}
|