Restriction of page requests



  • How can we limit the number of queries to the php page, for example, 3 in 1 sec, and how can this be better implemented?

    P.s The page itself contains dynamic JSON data



  • And you don't limit access, just cash the result by keeping it in operational memory, like memcached with a storage period of 3 seconds.

    <?php
    $m = new Memcached();
    $m->addServer('localhost', 11211);
    

    $json = 'error';
    if (!($json = $m->get('json'))) {
    if ($m->getResultCode() == Memcached::RES_NOTFOUND) {
    // Долго и трудоемко вычисляем JSON
    $json = '{...}';
    // Устанавливаем значение на 3 секунды
    $m->add('json', $json, 3);
    }
    }
    echo $json;

    As soon as the memcached removes json's key, the violator won't find it, reassess the dynamic request, put it in memcached and give it to the client. Between these events, json will be extracted from memcached very quickly. Then you will be able to give an arbitrary number of requests without putting pressure on the storage facility responsible for the JSON-a generation. Moreover, when the pressure is increasing, you will be able to increase the time of the key storage, and if it is reduced, on the contrary, to reduce.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2