Is it recommended to use PHP caching? How to use?

4

PHP caching is basically a cache created from your site to prevent it from processing data by fetching data for each page request. When a user accesses a page, a copy of it is saved in HTML on the server, this copy is valid at a certain time, after that time the original page is called and a new copy is created.

In theory, the application of this practice makes the site faster, I researched the subject and found some examples, follow what I liked the most:

<?php
$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = 'cached-'.substr_replace($file ,"",-4).'.html';
$cachetime = 18000;


if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
    include($cachefile);
    exit;
}
ob_start();

The above code is responsible for checking if the page is already cached, if it is then called it, if it does not exist, the script is still running I have its default content.

<?php
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); 

In this code, the cache file is created and opened and called to the browser.

Is it recommended to use this method?

In several cases we will have a login area, and each user will have access to different parts, when saving this cache the user would not end up getting data from the other one (displaying another's cache page)?

If yes, how to avoid?

And how to correctly use this technique (if I put it here is wrong)?

    
asked by anonymous 11.07.2016 / 05:43

1 answer

3

First: recommended where? In a site with few accesses or with millions of hits?

Cache is advantageous when there is a lot of access in certain patterns. It is not something magical that puts itself and the site is fast. It may even make a gain, but it will not be a real advantage. In some cases the site may slow down.

"To vary" the answer is that it has to measure. You have to do and simulate the access pattern to see if there is a significant gain. Need to identify bottlenecks and know where the cache needs to act. If there are no bottlenecks, the cache is irrelevant.

Cache brings malicious effects. If the information needs to be accurate it can not use cache. Unless the cache can be determined by the production of content and not for a given time. Whenever it is feasible I prefer this form, after all the cache can survive longer if nothing has changed and is invalidated as soon as it is needed.

Only one user content cache has little or no advantage. Then you need to see if you need this little advantage or not. A small gain multiplied by millions of people is significant, so half a dozen is too much effort to gain near zero. It depends on the usage pattern of the application.

If the cache is well done it will not fetch data from another user. If you're naive, you'll get it. It shows that it did not need caching. The basic solution to avoid this is to write a user identifier in the file name and indicate that only the user can access that cache.

There's even a cache on file, but it's not even close to something that only exists in memory. The reverse may happen in some cases.

This code may have a race condition that may just not help either, or cause a problem. It might not be a problem today, but it may take a day.

By the question you can not know if there will be additional problem. You can not understand the whole flow.

    
11.07.2016 / 12:47