OPcache deep dive
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.
if (function_exists("opcache_get_status")) {
$info = opcache_get_status(false);
echo $info["opcache_enabled"];
}
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.
var_dump(ini_get("opcache.validate_timestamps"));
var_dump(ini_get("opcache.revalidate_freq"));
echo "checked";
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.
opcache_compile_file("/app/src/bootstrap.php");
echo "preloaded";
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.
if (PHP_SAPI === "cli") {
echo ini_get("opcache.enable_cli");
}
Next: Static analysis
Article author: Arthur Isaev