问题
Pretty simple question, but I can't actually tell for sure. How does PHP behave with requires/includes of files that have been already cached by APC? My understanding is that this cache primarily saves PHP of compiling a file that has been already cached, but it's not clear to me whether it also saves the disk hit necessary to find/fetch the file aswell; so, does it? Or will PHP hit the disk, even if the opcode has been already cached, the only difference being that it doesn't do the compilation process again?
I would like to think requires/includes magically know a file is cached and thus fetch it directly from memory thanks to APC, but I just realized I have no reason to assume this is case, hence why I ask.
回答1:
I think your questions on this topic is explained in the APC manual, but it is sort of hidden. you must read the configuration part to find it. Especially check out the apc.stat configuration option which states this:
... This defaults to on, forcing APC to stat (check) the script on each request to determine if it has been modified. If it has been modified it will recompile and cache the new version. If this setting is off, APC will not check
...
For included/required files this option applies as well, but note that for relative path includes (any path that doesn't start with / on Unix) APC has to check in order to uniquely identify the file. If you use absolute path includes APC can skip the stat and use that absolute path as the unique identifier for the file.
So it looks like APC is caching required/included files in much the same way as the original script, if you include/require by the absolute path to the file. If you are using a relative path (which many do) it will require a disk hit to find out the full file name.
回答2:
Actually, APC does OPEN syscalls for the *once variants (include_once, require_once). You can check this easily using strace.
That is because the code for the *once is slighty different, calling zend_stream_open before calling compile_filename (which is overwritten by apc):
https://github.com/php/php-src/blob/master/Zend/zend_execute.h
There is an open issue in the bug tracker too: https://bugs.php.net/bug.php?id=59372
回答3:
APC overrides the function zend_compile_file in the Zend Engine, which among other things is responsible locating and opening the actual files; thanks to this it is able to "hijack" disk hits before they happen if the file is already cached.
Therefore, yes, files are served from memory if cached.
Sources: APC Technotes and the Zend Engine source code, specifically zend_language_scanner.c
来源:https://stackoverflow.com/questions/11301124/apc-disk-hits-and-requires-includes