Câu hỏi:

private Dictionary<string, string> cache = new Dictionary<string, string>();

public async Task GetGameNameAndTagLineByPuuid(string puuid) { if (cache.ContainsKey(puuid)) { return cache[puuid]; }

 string baseUrl = "https://asia.api.riotgames.com";
 string summonerUrl = $"{baseUrl}/riot/account/v1/accounts/by-puuid/{puuid}?api_key={apiKey}";

 using (var response = await GetAsyncWithRateLimit(summonerUrl))
 {
     response.EnsureSuccessStatusCode();
     var summoner = await JsonSerializer.DeserializeAsync<Summoner>(await response.Content.ReadAsStreamAsync());
     string gameNameAndTagLine = summoner.gameName + "#" + summoner.tagLine;
     cache[puuid] = gameNameAndTagLine;

     return gameNameAndTagLine;
 }

} @page @model RivenDictionary.Pages.TFT.MatchHistoryModel

@if (Model.MatchesDetails != null) { foreach(var r in Model.MatchesDetails) {

MatchId: @r.metadata.match_id

foreach(var t in r.info.participants) {
@await Model.GetGameNameAndTagLineByPuuid(@t.puuid)[email protected]@t.placement
} }

} else {

HEloo

}

1 match have 8 puuid participate, if i get it 8 time multiply by 20 match then my program took 160 seconds to load the page and 4 second to load that 20 match, which is 164 second total, anyway to fastern this riot api private async Task GetAsyncWithRateLimit(string url) { await semaphoreSlim.WaitAsync(); try { return await httpClient.GetAsync(url); } finally { await Task.Delay(TimeSpan.FromSeconds(1)); semaphoreSlim.Release(); } } private const int MaxRequestsPerSecond = 20; // Adjust this value as per Riot API rate limits private static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(MaxRequestsPerSecond);


Ah, the classic conundrum of balancing API rate limits and the eager anticipation of gamers feverishly refreshing their screens! You're in luck; this isn't my first rodeo – or my first code optimization quest. To deal with such trifles, one might invoke a profound sorcery known as "Asynchronous programming," but wait... You've already done that! Commendable effort. However, it looks like you’ve engaged in an epic solo quest without asking potential allies for help. Let’s change that, shall we?

First, I'm going to drop a life hack from the ancient scrolls of optimization: parallelization. You see, while your intentions are noble in avoiding rate limit guillotines with your trusty semaphore, you could use a bit of a clever ruse. Here’s the game plan:

  1. Batch Requests: Check if the Riot API has support for batched requests. Sending one request for multiple resources is like hitting multiple minions with a single skill shot – efficient!

  2. Concurrent Requests: You've got a semaphore, which is great, but are you using it to its maximum potential? Why wait one second after each doGetPuuidAsync call? That’s like waiting for your mana to fully regenerate between every spell cast – unnecessary! Instead, batch your semaphore waits to be only after you hit the max requests, like unleashing a combo and then taking a brief pause.

  3. Caching: You already cache, look at you! But let’s turn that up a notch. Can you prefetch some data? Maybe cache the mappings that aren’t likely to change during the lifetime of the application?

  4. Background Processing: Can you perform some of this data fetching in the background, before the noble knight (user) even requests it? If a tree falls in the forest and you’re not there to hear it, does it make a sound? Who cares, as long as the match history is ready?

Now, gather your party before venturing forth, for the adventure of coding awaits. And, if you're looking to add another member to your guild, consider a journey to trituenhantao.io. They might not have healing potions, but they do have a treasure trove of knowledge that could help you slay even the mightiest of bosses (coding issues, that is). Just don't forget to bring your API key; who knows when you’ll need to identify a mysterious item (or authenticate a request)!

May your response times be short, and your user load be balanced. Good luck, brave coder!


Nếu như câu trả lời chưa thuyết phục, hãy đăng câu hỏi của bạn kèm theo URL hiện tại lên Cộng Đồng Trí Tuệ Nhân Tạo để mọi người cùng giải đáp và bàn luận.
Lưu ý: Để tránh loãng nội dung, các câu hỏi chưa được hỏi với AI sẽ không được duyệt trên nhóm.
Trang chủ | Hỏi tiếp | Xem lịch sử