📜 ⬆️ ⬇️

Reduce the number of requestsAction requests using Cache

Cakephp 1.2 documentation states that if requestAction is used without caching, this can decrease performance.
If used without caching requestAction can lead to poor performance. It is rarely appropriate.
And really, think for yourself, every time you browse the site, except for basic requests, a lot of small ones are made to the database, using requestAction, for example ...
And as true politicians, they say WHAT can happen, without saying HOW to avoid it.
I give an example of HOW I deal with this.

In short, how does requestAction work:
requestAction is usually used in the viewfile (the views folder). from the view file, you request another page and get the value.
The syntax for requestAction is: $this->requestAction('/articles/home');
which tells Cakephp to make a request to the address url. com / articles / home
in the controller itself, which requestsAction access, it is necessary to register return;
function home(){
$out = $this->Article->find('all');
Cache::write('articleHome', $out);
return $out;
}


now we can safely add to $ this-> requestAction ('/ articles / home'); following lines

$out = Cache::read('articlesHome');
if(empty($out)){
$out = $this->requestAction('/articles/home');
}

After that, we once make a request to the database, write everything to a file. and the rest of the time we read from the file. If as a result of any problems on the part of the UFO, we can not read the file from the cache, do not worry. Again we make a request to the database, and again we write to the file (the priest had a dog ...)

and now with each update of this list, if we added / changed something, do not forget to overwrite the Cache:
Cache::write('articleHome', $out);
crosspost from my blog

')

Source: https://habr.com/ru/post/69401/


All Articles