66 lines
2.0 KiB
C#
66 lines
2.0 KiB
C#
using KLHZ.Trader.Core.Contracts.Declisions.Dtos;
|
|
using KLHZ.Trader.Core.Contracts.Declisions.Dtos.Enums;
|
|
|
|
namespace KLHZ.Trader.Core.Math.Declisions.Dtos
|
|
{
|
|
internal class TimeWindowCacheItem
|
|
{
|
|
private readonly object _locker = new();
|
|
private readonly LinkedList<CachedValue> _cachedValues = new();
|
|
|
|
public readonly TimeSpan WindowSize;
|
|
public readonly string Key;
|
|
|
|
public TimeWindowCacheItem(string key, TimeWindowCacheType window)
|
|
{
|
|
Key = key;
|
|
WindowSize = GetTimeSpan(window);
|
|
}
|
|
|
|
public ValueTask AddData(CachedValue cachedValue)
|
|
{
|
|
lock (_locker)
|
|
{
|
|
_cachedValues.AddLast(cachedValue);
|
|
while (_cachedValues.Last != null && _cachedValues.First != null
|
|
&& _cachedValues.Last.Value.Time - _cachedValues.First.Value.Time > WindowSize)
|
|
{
|
|
_cachedValues.RemoveFirst();
|
|
}
|
|
}
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
|
|
public ValueTask<CachedValue[]> GetValues()
|
|
{
|
|
lock (_locker)
|
|
{
|
|
return ValueTask.FromResult(_cachedValues.ToArray());
|
|
}
|
|
}
|
|
|
|
private static TimeSpan GetTimeSpan(TimeWindowCacheType type)
|
|
{
|
|
switch (type)
|
|
{
|
|
case TimeWindowCacheType._5_Minutes:
|
|
{
|
|
return TimeSpan.FromMinutes(5);
|
|
}
|
|
case TimeWindowCacheType._15_Minutes:
|
|
{
|
|
return TimeSpan.FromMinutes(15);
|
|
}
|
|
case TimeWindowCacheType._20_Seconds:
|
|
{
|
|
return TimeSpan.FromSeconds(20);
|
|
}
|
|
default:
|
|
{
|
|
return TimeSpan.FromMinutes(1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|