| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- using Application.Abstractions.Messaging;
- using Application.Abstractions.Cache;
- using Application.Abstractions.Crypto;
- namespace Application.Features.Api.Crypto.Ticker.GetDetail;
- public sealed class Handler(IUpbitClient upbit, ICacheService cache) : IQueryHandler<Query, Response>
- {
- private static readonly TimeSpan _cacheExpiry = TimeSpan.FromSeconds(10);
- public async Task<Response> Handle(Query request, CancellationToken ct)
- {
- var market = $"KRW-{request.Symbol.ToUpper()}";
- var cacheKey = CacheKeys.CryptoTickerDetail(request.Symbol);
- // 캐시 확인
- var cached = await cache.GetAsync<Response>(cacheKey, ct);
- if (cached is not null)
- {
- return cached;
- }
- // Upbit REST API 호출
- var tickers = await upbit.GetTickersAsync([market], ct);
- if (tickers.Count == 0)
- {
- return new Response(market, 0, "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0, "", 0);
- }
- var t = tickers[0];
- var response = new Response(
- t.Market,
- t.TradePrice,
- t.Change,
- t.SignedChangePrice,
- t.SignedChangeRate,
- t.OpeningPrice,
- t.HighPrice,
- t.LowPrice,
- t.PrevClosingPrice,
- t.AccTradePrice,
- t.AccTradePrice24h,
- t.AccTradeVolume,
- t.AccTradeVolume24h,
- t.Highest52WeekPrice,
- t.Highest52WeekDate,
- t.Lowest52WeekPrice,
- t.Lowest52WeekDate,
- t.Timestamp
- );
- // Redis 캐시 저장 (10초)
- await cache.SetAsync(cacheKey, response, _cacheExpiry, ct);
- return response;
- }
- }
|