Good day. There is a site that works on the principle file_get_contents. (takes information from Web Api). There is a problem. With a quick click on F5, the site hangs for a while, because probably not coping with the loads. Is it possible to somehow reduce the load on the pages where this function is used? If there are many people sitting on the site, it seems to me that it will explode altogether. I would be grateful for the help.

Information output code: https://gist.github.com/Xhonor/f6720ec44eef783af9625dcf31d62950

The code where the information comes from: https://gist.github.com/Xhonor/469ba3f45d6660049d5e7e81a5a03bba

Everything works on Steam Web Api

  • You need to optimize the code (surprised, yes?) - and for this you need to show this code (surprised, yes?), Preferably immediately show a piece that, according to measurements, eats the most time. - AK
  • Ooh, I'll be a little telepathic! Judging by the page, do you calculate all the parameters of the profile on the fly, even any heavyweight ones and have you not heard such a word like “cache the values ​​for half an hour”? - AK
  • one
    You say so, as if I am a professional programmer and just turned on the fool. I'm just an amateur who needs advice. - Xhonor
  • What part of the code do you need? Where is the result displayed or how information is taken from the site? - Xhonor
  • one
    Measure the total time of page generation - once. You have ~ five calls to external service, apparently there will be major brakes on obtaining information. The rest should not contribute very much - although I did not read all the code, you have so much copy-paste that I scrolled on the machine. I don’t know, maybe they didn’t tell you - but when you make a copy of a string with elseif and change names in two places, it’s not really programming. - AK

1 answer 1

There is such a thing, called network latency - the time spent on traveling by wire. It is very slow, and the longer the distance the slower (the speed of light). Even if we compare the speed of access to localhost and next to the same computer. The difference in request speed will be up to 20 ms.

I suspect steam hosts its api server at a considerable distance from your provider, i.e. Each request will cost you at best 100ms one way.

To drive back - 200ms (optimistic), this is for everyone! request to api . Simple math says that more than 5 queries per second can not be done.

Change the code to make less requests to api . Store information in session or cache ...

Cache - memcached , redis . These applications allow you to store data for a specific time (half an hour for example).

With the session harder, most will have to manage time.

Example:

 <?php> // Псевдо код для понимания а не копирования!!! function getUserInfo($userId) { // Пробуем читать из кэша/сессии $userInfo = $cache->read("user_info_" . $userId); // Если в кэше нет if (!$userInfo) { // Читаем с api. $userInfo = $api->read("user_info_" . $userId); // Пишем в кэш на 30 минут $cache->write("user_info_" . $userId, $userInfo, 30); } // Возвращаем значение. return $userInfo; } // Пишем информацию о пользователе function setUserInfo($userId, $userInfo) { // Пишем новые значения в api. $api->write("user_info_" . $userId, $userInfo); // Пишем новые значения в кэш. Важно что бы небыло старых данных $cache->write("user_info_" . $userId, $userInfo, 30); } 
  • Thank you. Difficult for me, but I will try) - Xhonor
  • And what do you think, can try to add data to mysql, if a new user is found? And add a button to update the data (once an hour for example). - Xhonor
  • @Xhonor Any way that reduces the number of requests will be good. MySQL in this case is also a cache, only slower (if compared with memcache, redis) to compare, but obviously faster than the API. Yes, and the button is suitable, depends on the application. - E_p