Add cache for position

This commit is contained in:
2025-10-10 02:48:50 +07:00
parent 5a91c0fdd1
commit 3128e3e9d9
2 changed files with 40 additions and 9 deletions

View File

@@ -480,8 +480,23 @@ public class DataController : ControllerBase
decimal totalVolume = strategy.Volume;
decimal volumeLast24h = strategy.Volume;
// Fetch positions associated with this bot using the provided trading service
var positions = await tradingService.GetPositionsByInitiatorIdentifierAsync(strategy.Identifier);
// Use caching for position data in UI context (not critical trading operations)
var cacheKey = $"positions_{strategy.Identifier}";
var cachedPositions = _cacheService.GetValue<List<Position>>(cacheKey);
List<Position> positions;
if (cachedPositions != null)
{
positions = cachedPositions;
}
else
{
// Fetch positions associated with this bot using the provided trading service
positions = (await tradingService.GetPositionsByInitiatorIdentifierAsync(strategy.Identifier)).ToList();
// Cache positions for 2 minutes for UI display purposes
_cacheService.SaveValue(cacheKey, positions, TimeSpan.FromMinutes(2));
}
// Calculate win/loss statistics from actual positions (including open positions)
int wins = positions.Count(p => p.ProfitAndLoss != null && p.ProfitAndLoss.Realized > 0);