Handler.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Application.Abstractions.Messaging;
  2. using Application.Abstractions.Cache;
  3. using Application.Abstractions.Crypto;
  4. namespace Application.Features.Api.Crypto.Market.GetAll;
  5. public sealed class Handler(IUpbitClient upbit, ICacheService cache) : IQueryHandler<Query, Response>
  6. {
  7. private static readonly TimeSpan _cacheExpiry = TimeSpan.FromMinutes(30);
  8. public async Task<Response> Handle(Query request, CancellationToken ct)
  9. {
  10. var cacheKey = CacheKeys.CryptoMarkets;
  11. // 캐시 확인
  12. var cached = await cache.GetAsync<List<Response.MarketItem>>(cacheKey, ct);
  13. if (cached is { Count: > 0 })
  14. {
  15. return new Response(cached);
  16. }
  17. // Upbit REST API 호출
  18. var markets = await upbit.GetMarketsAsync(ct);
  19. var items = markets
  20. .Select(m => new Response.MarketItem(
  21. m.Market,
  22. m.KoreanName,
  23. m.EnglishName,
  24. new Response.MarketEvent(
  25. m.MarketEvent.Warning,
  26. new Response.MarketCaution(
  27. m.MarketEvent.Caution.PriceFluctuations,
  28. m.MarketEvent.Caution.TradingVolumeSoaring,
  29. m.MarketEvent.Caution.DepositAmountSoaring,
  30. m.MarketEvent.Caution.GlobalPriceDifferences,
  31. m.MarketEvent.Caution.ConcentrationOfSmallAccounts
  32. )
  33. )
  34. ))
  35. .ToList();
  36. // Redis 캐시 저장 (30분)
  37. if (items.Count > 0)
  38. {
  39. await cache.SetAsync(cacheKey, items, _cacheExpiry, ct);
  40. }
  41. return new Response(items);
  42. }
  43. }