| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- using Application.Abstractions.Messaging;
- using Application.Abstractions.Cache;
- using Application.Abstractions.Crypto;
- namespace Application.Features.Api.Crypto.Trade.GetRecent;
- public sealed class Handler(IUpbitClient upbit, ICacheService cache) : IQueryHandler<Query, Response>
- {
- private static readonly TimeSpan _cacheExpiry = TimeSpan.FromSeconds(5);
- public async Task<Response> Handle(Query request, CancellationToken ct)
- {
- var count = Math.Clamp(request.Count, 1, 100);
- var market = $"KRW-{request.Symbol.ToUpper()}";
- var cacheKey = CacheKeys.CryptoTrades(request.Symbol);
- // 캐시 확인
- var cached = await cache.GetAsync<List<Response.TradeItem>>(cacheKey, ct);
- if (cached is { Count: > 0 })
- {
- return new Response(cached);
- }
- // Upbit REST API 호출
- var trades = await upbit.GetTradesAsync(market, count, ct);
- var items = trades
- .Select(t => new Response.TradeItem(
- t.Timestamp,
- t.TradePrice,
- t.TradeVolume,
- t.AskBid,
- t.SequentialId
- ))
- .ToList();
- // Redis 캐시 저장 (5초)
- if (items.Count > 0)
- {
- await cache.SetAsync(cacheKey, items, _cacheExpiry, ct);
- }
- return new Response(items);
- }
- }
|