0

To minimize page loading time, we are caching our PHP generated HTML as an HTML file. If a "cache file" exists we use it. Otherwise we run the PHP scripts to generate the page. To reduce the page loading speed even more, we would like to remove the "If file exists" line (which takes time) and simply let PHP generate an error if the file does not exist. We would then "catch" the error and let PHP generate the page.

The question is the following: Is there a simple way to implement "On error, leave function" in PHP.

This is the code we have:

function UseCachedPage()
{
$CacheFile='mypage.html';
if(!file_exists($CacheFile)) return; //Would like to skip this to save time
include($CacheFile); 
//Magic "on error, leave function" needed here.
exit; //Cache included, so no need to run the php.
}

Any help would be appreciated.

Cymro
  • 1,201
  • 1
  • 11
  • 29
  • I mean.. what kind of error? PHP will fatal error/throw notices/warnings if the PHP isn't valid – treyBake Nov 13 '19 at 13:38
  • 2
    _“(which takes time)”_ - have you actually measure how much that is …? Sorry, but the whole idea sounds like high-grade nonsense to me. I very much doubt this will speed anything up significantly; depending on how exactly you plan on “handling the error”, it might even slow things down more. – 04FS Nov 13 '19 at 13:40
  • https://stackoverflow.com/questions/8261756/how-to-catch-error-of-require-or-include-in-php?noredirect=1&lq=1 – AbraCadaver Nov 13 '19 at 13:42
  • We try to include the cache file. If it's there, great. If not PHP will generate this error": Warning: include('cache file') [function.include]: failed to open stream: No such file or directory in xx.php on line xx Warning: include() [function.include]: Failed opening 'xxx.html' for inclusion (include_path='.:/usr/local/lib/php/') in xx.php on line xx – Cymro Nov 13 '19 at 13:44
  • 1
    How about using specialized tools for caching, like i.e. Varnish? – MilanG Nov 13 '19 at 13:45
  • 04FS, thank you for your comment. milisecond improvements make a big deal on google ranking. So, yes, it is important. And the "slowing things down even more" would only happen if a cache file did not exist, which is almost never. So, there is indeed an improvement in page loading speed. – Cymro Nov 13 '19 at 13:47
  • _“So, there is indeed an improvement in page loading speed.”_ - I believe that when you can back it up with actual measurements … – 04FS Nov 13 '19 at 13:48
  • "There are only two hard things in Computer Science: cache invalidation and naming things." -- Phil Karlton – Progrock Nov 13 '19 at 13:48
  • 04FS, if "file_exists()" takes time, than I am right, and striving to remove this time from page load is a worthy endevour. – Cymro Nov 13 '19 at 13:53
  • @Cymro, we know nothing about your caching mechanism, their are many possible implementations. But if file lookups are an issue, how about in-memory caching? – Progrock Nov 13 '19 at 13:57
  • Progrock, "in-memory caching"? Sounds cool. Cheeky of me but could you provide a link for this? – Cymro Nov 13 '19 at 14:00
  • Best search for the favourite 'in memory' caching technique/too/software of the day. – Progrock Nov 13 '19 at 15:03

1 Answers1

3

According to the docs:

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.

So what this means is if you want to "catch" it, you have to use require instead. Now, you can only catch this fatal error as of PHP 7, which is important to know.

function UseCachedPage() {
    $CacheFile = "mypage.html";
    try {
        require($CacheFile);
        exit;
    } catch (Error $e) {
        // Call something to build your file here
    }
}
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • 1
    Good reminder about possibly traversing include paths, make sure you use absolute paths for the require. Or there might be a lot of file traversing going on, try here, try here, try here, try here etc. – Progrock Nov 13 '19 at 14:06