Handler.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Cache;
  3. using Application.Abstractions.Crypto;
  4. namespace Application.Features.Api.Crypto.Ticker.GetDetail;
  5. public sealed class Handler(IUpbitClient upbit, ICacheService cache) : IQueryHandler<Query, Response>
  6. {
  7. private static readonly TimeSpan _cacheExpiry = TimeSpan.FromSeconds(10);
  8. public async Task<Response> Handle(Query request, CancellationToken ct)
  9. {
  10. var cacheKey = CacheKeys.CryptoTickerDetail(request.Market);
  11. // 캐시 확인
  12. var cached = await cache.GetAsync<Response>(cacheKey, ct);
  13. if (cached is not null)
  14. {
  15. return cached;
  16. }
  17. // Upbit REST API 호출
  18. var tickers = await upbit.GetTickersAsync([request.Market], ct);
  19. if (tickers.Count == 0)
  20. {
  21. return new Response(request.Market, "", "", "", "", 0, 0, 0, 0, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0, "", 0);
  22. }
  23. var t = tickers[0];
  24. var response = new Response(
  25. t.Market,
  26. t.TradeDate,
  27. t.TradeTime,
  28. t.TradeDateKst,
  29. t.TradeTimeKst,
  30. t.TradeTimestamp,
  31. t.OpeningPrice,
  32. t.HighPrice,
  33. t.LowPrice,
  34. t.TradePrice,
  35. t.PrevClosingPrice,
  36. t.Change,
  37. t.ChangePrice,
  38. t.ChangeRate,
  39. t.SignedChangePrice,
  40. t.SignedChangeRate,
  41. t.TradeVolume,
  42. t.AccTradePrice,
  43. t.AccTradePrice24h,
  44. t.AccTradeVolume,
  45. t.AccTradeVolume24h,
  46. t.Highest52WeekPrice,
  47. t.Highest52WeekDate,
  48. t.Lowest52WeekPrice,
  49. t.Lowest52WeekDate,
  50. t.Timestamp
  51. );
  52. // Redis 캐시 저장 (10초)
  53. await cache.SetAsync(cacheKey, response, _cacheExpiry, ct);
  54. return response;
  55. }
  56. }