| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using Application.Abstractions.Messaging;
- using Application.Abstractions.Cache;
- using Application.Abstractions.Crypto;
- namespace Application.Features.Api.Crypto.Candle.GetWeeks;
- public sealed class Handler(IUpbitClient upbit, ICacheService cache) : IQueryHandler<Query, Response>
- {
- private static readonly TimeSpan _cacheExpiry = TimeSpan.FromMinutes(5);
- public async Task<Response> Handle(Query request, CancellationToken ct)
- {
- var count = Math.Clamp(request.Count, 1, 200);
- var market = $"KRW-{request.Symbol.ToUpper()}";
- var cacheKey = CacheKeys.CryptoCandle(request.Symbol, "w");
- // 캐시 확인
- var cached = await cache.GetAsync<List<Response.CandleItem>>(cacheKey, ct);
- if (cached is { Count: > 0 })
- {
- return new Response(cached);
- }
- // Upbit REST API 호출
- var candles = await upbit.GetWeekCandlesAsync(market, count, ct);
- var items = candles
- .OrderBy(c => c.CandleDateTimeUtc)
- .Select(c => new Response.CandleItem(
- new DateTimeOffset(c.CandleDateTimeUtc, TimeSpan.Zero).ToUnixTimeSeconds(),
- c.OpeningPrice,
- c.HighPrice,
- c.LowPrice,
- c.TradePrice,
- c.CandleAccTradeVolume,
- c.CandleAccTradePrice
- ))
- .ToList();
- // Redis 캐시 저장 (5분)
- if (items.Count > 0)
- {
- await cache.SetAsync(cacheKey, items, _cacheExpiry, ct);
- }
- return new Response(items);
- }
- }
|