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

Asked

Viewed 1,689 times

6

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

In theory, the application of this practice makes the site faster, I researched on the subject and found some examples, follow what I liked 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 you have already cached that page, if it exists then it is called, if it does not exist, the script keeps being run I have its default content.

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

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

It is recommended to use this method?

In several cases we will have an area of login, and each user will have access to different parts, when saving this cache the user would not end up taking data from the other (displaying the other’s cache page)?

If yes, how to avoid?

And how to use this technique correctly (in case the one I put here is wrong)?

  • 1

    If the page can be accessed by all users and is the same page without modifications I believe CDN could use Cloudflare or Incapsula (the latter promises a better performance in these cases) or can use Sucuri. They are paid solutions, but they are not so expensive (Cloudflare has a free plan, but I do not know if it offers this feature), in the paid alternatives we have Sucuri for ~10 dollars, Cloudflare for ~20 dollars and Incapsula for ~40 dollars, unless mistaken. It’s just a suggestion beyond the question! Dynamic data (comments) can be loaded by JS. ;)

1 answer

5


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

Cache is advantageous when there is a lot of access in certain patterns. It is not something magical that is put and the site is fast. It may even give a gain, but not be a real advantage. In some cases the site may get slower. This site here that is using now (one of the 30 most accessed in the world) had a lot of cache, one day they decided to shut down some of them and got faster.

"For a change" the answer is that it has to measure (the OS used without measuring and believed to have won when it had loss). You have to do and simulate the access pattern to see if there is a significant gain. You need to identify bottlenecks and know where the cache needs to act. If there are no bottlenecks, the cache is irrelevant.

Cache brings harm. If the information needs to be accurate you cannot use cache. Unless the cache can be determined by the production of content and not for a certain 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 necessary. But this generates a cost so great that ends up not compensating in most cases.

Caching only one user’s content has little or no advantage. Then you need to see if you need this small advantage or not. A small gain multiplied by millions of people is something significant, for half a dozen is too much effort for almost zero gain. Depends on the application usage pattern.

If the cache is well made it will not take data from another user. If it is naive, it will take it. It shows that it did not need cache. The basic solution to avoid this is to write a user identifier to the file name and indicate that only it can access that cache.

There’s even gain in file caching, but it’s not even close to something that only exists in memory. It may be otherwise in some cases.

That code may have a running condition which may just not help as much, or cause trouble. It may not be a problem today, but it may be a day.

By the question it is not possible to know if there will be additional problem. It is not possible to understand the whole flow.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.