0

I'm working (for fun), with an API (Riot API), and I made something to retrieve match histories (it's in the game). And I've a problem, everything works really fine, but I don't know how to optimize it, what I mean is : I do the call every time the user refresh the page, and it takes loooong looong time. I tried to call it with AJAX, but didn't find a good way, AJAX dont find my objects.

Here is the code :

$match_history = [];
$api = "";

if (!empty($region)) {
    try {
        $api = new App\Library\RiotAPI\RiotAPI([
            App\Library\RiotAPI\RiotAPI::SET_KEY             => 
App\Config::API_KEY,
            App\Library\RiotAPI\RiotAPI::SET_CACHE_RATELIMIT => true,
            App\Library\RiotAPI\RiotAPI::SET_CACHE_CALLS     => true,
            App\Library\RiotAPI\RiotAPI::SET_REGION          => 
App\Library\RiotAPI\Definitions\Region::getRegion($region),
        ]);
    } catch (\Exception $e) {
        // die($e->getMessage());
    }
}

if ($api) {
// Insert current rank etc...
try {
    $summoner = $api- 
>getSummonerByName(App\Repository\UserRepository::getInstance()- 
>getUserDetail($_SESSION['user']['id'], 'summoner_name'));
} catch (\Exception $e) {
    $summoner = null;
}
// Match history
if (!empty($summoner)) {
    try {
        $matches = $api->getRecentMatchlistByAccount($summoner->accountId);

        // For every matches
        foreach ($matches as $match) {
            $a_match = $api->getMatch($match->gameId);

            if ($a_match->gameType === "MATCHED_GAME") {
                $gameCreation = date("d-M-Y H:i:s", substr($a_match- 
>gameCreation, 0, 10));
                if ($gameCreation >= date("d-M-Y", 
strtotime($user['created_at']))) {
                    // Get the participant ID of the customer
                    foreach ($a_match->participantIdentities as 
$participantIdentity) {
                        if ($participantIdentity->player->currentAccountId 
=== $summoner->accountId) {
                            $participantId = $participantIdentity- 
>participantId;
                        }
                    }

                    // Get stats of the participant
                    foreach ($a_match->participants as $participant) {
                        if ($participant->participantId === $participantId) 
{
                            $match_history[$match->gameId]['gameCreation'] = 
$gameCreation;
                            $match_history[$match->gameId]['championId'] = 
$participant->championId;
                            $match_history[$match->gameId]['spells'] 
['spell1'] = $participant->spell1Id;
                            $match_history[$match->gameId]['spells'] 
['spell2'] = $participant->spell2Id;
                            $match_history[$match->gameId]['win'] = 
$participant->stats->win;
                            $match_history[$match->gameId]['kills'] = 
$participant->stats->kills;
                            $match_history[$match->gameId]['deaths'] = 
$participant->stats->deaths;
                            $match_history[$match->gameId]['assists'] = 
$participant->stats->assists;
                            $match_history[$match->gameId]['goldEarned'] = 
$participant->stats->goldEarned;
                            $match_history[$match->gameId] 
['totalMinionsKilled'] = $participant->stats->totalMinionsKilled;
                            $match_history[$match->gameId]['items']['item0'] 
= $participant->stats->item0;
                            $match_history[$match->gameId]['items']['item1'] 
= $participant->stats->item1;
                            $match_history[$match->gameId]['items']['item2'] 
= $participant->stats->item2;
                            $match_history[$match->gameId]['items']['item3'] 
= $participant->stats->item3;
                            $match_history[$match->gameId]['items']['item4'] 
= $participant->stats->item4;
                            $match_history[$match->gameId]['items']['item5'] 
= $participant->stats->item5;
                            $match_history[$match->gameId]['items']['item6'] 
= $participant->stats->item6;
                        }
                    }
                }
            }
        }

    } catch (\Exception $e) {
        // die($e->getMessage());
    }
 }
}

I would like to know if there's a way to : - Run it in background, without AJAX or something like : Keep in mind the match_history for X time, and then after X time, do the call again when the user refresh the page.

Thanks for your help! Best Regards.

Théo Benoit
  • 567
  • 1
  • 4
  • 14
  • If you're looking up the same information repeatedly you can use something like `memcached` to save the answer locally. – Barmar Mar 29 '18 at 20:04
  • Hey @Barmar, thanks for your answer, I made some research on it, and I don't see how to use it there, could you help me a little bit ? Thanks you sir – Théo Benoit Mar 29 '18 at 20:15
  • Why do you break your lines between the `-` and `>` in `->`? That's perverse! – Barmar Mar 29 '18 at 20:19
  • I see James, I see, I'm sorry, I'm kinda disapointed because I tryed a lot to optimize it and I don't see how ^^ – Théo Benoit Mar 29 '18 at 20:21
  • @Barmar, That stress me too, but it's when I copyed paste the code in stackoverflow, it did that aaaaaah – Théo Benoit Mar 29 '18 at 20:21
  • See https://stackoverflow.com/editing-help for how to paste code without messing it up. – Barmar Mar 29 '18 at 20:23

0 Answers0