2024-03-21 15:26:48 +00:00
|
|
|
|
namespace NethereumWorkflow.BlockUtils
|
|
|
|
|
{
|
|
|
|
|
public class BlockCache
|
|
|
|
|
{
|
|
|
|
|
public delegate void CacheClearedEvent();
|
|
|
|
|
|
2024-03-22 13:16:55 +00:00
|
|
|
|
private const int MaxEntries = 1024 * 1024 * 5;
|
2024-03-21 15:26:48 +00:00
|
|
|
|
private readonly Dictionary<ulong, BlockTimeEntry> entries = new Dictionary<ulong, BlockTimeEntry>();
|
|
|
|
|
|
|
|
|
|
public event CacheClearedEvent? OnCacheCleared;
|
|
|
|
|
|
|
|
|
|
public BlockTimeEntry Add(ulong number, DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
return Add(new BlockTimeEntry(number, dateTime));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BlockTimeEntry Add(BlockTimeEntry entry)
|
|
|
|
|
{
|
|
|
|
|
if (!entries.ContainsKey(entry.BlockNumber))
|
|
|
|
|
{
|
|
|
|
|
if (entries.Count > MaxEntries)
|
|
|
|
|
{
|
|
|
|
|
entries.Clear();
|
|
|
|
|
var e = OnCacheCleared;
|
|
|
|
|
if (e != null) e();
|
|
|
|
|
}
|
|
|
|
|
entries.Add(entry.BlockNumber, entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entries[entry.BlockNumber];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BlockTimeEntry? Get(ulong number)
|
|
|
|
|
{
|
|
|
|
|
if (!entries.TryGetValue(number, out BlockTimeEntry? value)) return null;
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2024-04-01 11:46:30 +00:00
|
|
|
|
|
|
|
|
|
public int Size { get { return entries.Count; } }
|
2024-03-21 15:26:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|