OPcache deep dive
OPcache keeps compiled PHP scripts in shared memory, so a request skips the parser. The examples below read the settings and the status of this machine, and the page says which branch the command line takes when the cache is off there.
opcache_get_status
Function opcache_get_status reports memory use, hit rates, and cached script counts. It is the fastest way to confirm the cache is active.
Call it without arguments for the full report. Guard the call with function_exists so scripts also run where OPcache is missing.
<?php if (function_exists("opcache_get_status")) { $info = opcache_get_status(); var_dump($info); }
php status.php
bool(false)
The function exists because the extension is loaded, and the call returns false because this command line runs with opcache.enable_cli off. Started with php -d opcache.enable_cli=1, the same call reports "opcache_enabled" => bool(true) and the memory numbers of the run.
validate_timestamps
Timestamp validation decides whether OPcache checks files for changes. Production often disables it for speed and refreshes the cache on deploy.
Stale files after deploy are the classic symptom, explained under
opcache staleness.
Check both settings below when a fix seems to have no effect.
<?php var_dump(ini_get("opcache.validate_timestamps")); var_dump(ini_get("opcache.revalidate_freq")); echo "checked", PHP_EOL;
php stamps.php
string(1) "1"
string(1) "2"
checked
Validation is on and the frequency is two seconds on this build, so an edited file is noticed within two seconds. A server with the first value at zero answers the same two calls with "0", and only a restart, an opcache_reset() or an invalidate call replaces the code it runs.
Preloading concept
Preloading compiles chosen files once at server start and keeps them in shared memory. Frameworks use it to warm up stable code before traffic arrives.
You list files in a preload script and point the server at it. Changed files take effect only after a restart.
<?php $file = __DIR__ . "/bootstrap.php"; file_put_contents($file, "<?php\nfunction boot(): string\n{\n return \"ready\";\n}\n"); if ((int) ini_get("opcache.enable_cli") === 1) { var_dump(opcache_compile_file($file)); } else { echo "opcache is off for the command line", PHP_EOL; } echo file_exists($file) ? "script ready" : "script missing", PHP_EOL;
php preload.php
opcache is off for the command line
script ready
The block writes the file it wants to compile and then asks the setting which world it is in. Run with php -d opcache.enable_cli=1, the same program prints bool(true) and script ready, because the cache compiled the file and the function became available.
OPcache in CLI
Command line PHP usually runs without OPcache because each process is short lived. The cache would be built and thrown away on every run.
Setting opcache.enable_cli turns it on for benchmarks and long workers. Leave it off for normal one shot scripts.
<?php if (PHP_SAPI === "cli") { echo ini_get("opcache.enable_cli"), PHP_EOL; }
php cli.php
0
Zero is the switch in its off state, and the guard keeps the print out of a web request where the value would not describe the process that answers.
Exercises
- Run the status example with php -d opcache.enable_cli=1 status.php and read the keys of the array.
- Print opcache_get_configuration() and find the directive that limits memory.
- Change one byte of a script and describe what a server with validation off does with it.
- Compare the run time of a loop with and without opcache.enable_cli=1 using microtime(true).
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does OPcache keep between requests?
Answer
Compiled scripts in shared memory
The compiler runs once and the byte code is reused, so the parser is skipped on a hit.
-
What does
opcache.validate_timestamps = 0mean for a changed file?Answer
The change is only seen after the cache is restarted or reset
Without validation the cache never looks at the file time, which is the classic stale deploy.
-
When does preloading compile its files?
Answer
Once, when the server starts
The server runs the preload script at start, and the compiled files stay in shared memory.
-
Why is OPcache usually off for command line PHP?
Answer
The cache would be built and discarded inside one short run
A one shot script gains nothing from a cache that dies with the process; long workers are the exception.
-
What does this program print?
<?php if (function_exists("opcache_get_status")) { var_dump(opcache_get_status() === false); }Answer
bool(true)
The function exists, and on a command line with the cache off it returns false, so the comparison is true.
-
What does this program print?
<?php echo (int) ini_get("opcache.validate_timestamps");Answer
1
Validation is on in this build, and the cast turns the string into its number.
-
What does this program print?
<?php if (PHP_SAPI === "cli") { echo "command line"; } else { echo "web"; }Answer
command line
The constant names the interface of the running process, and a script started by the php binary is cli.
-
What does this program print?
<?php $before = (int) ini_get("opcache.enable_cli"); echo $before === 0 ? "off" : "on";Answer
off
The command line switch is off in this image, so the integer is zero and the ternary prints off.
-
A deploy copies new files and the site still runs the old code. What is the first setting to read?
Answer
opcache.validate_timestampsWith validation off the cache keeps the old byte code until a restart or an
opcache_reset(). -
Why does the status example guard the call with
function_exists()?Answer
Because the extension may be missing and the call would then be fatal
An undefined function is a fatal error, and a small tool may run where OPcache is not installed.
The examples call function_exists(), ini_get(). The programs on this page were run with PHP 8.5.13 (php -v) with the commands shown above.
Article author: Arthur Isaev