From 8eb4085bcd8a26dee940a045dd2d838791e328d0 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Mon, 22 Sep 2025 11:02:12 -0600 Subject: [PATCH] opcache improvements for first hit Signed-off-by: Andy Miller --- .../src/Grav/Common/Config/CompiledBase.php | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/system/src/Grav/Common/Config/CompiledBase.php b/system/src/Grav/Common/Config/CompiledBase.php index 2c757744c..760978f8b 100644 --- a/system/src/Grav/Common/Config/CompiledBase.php +++ b/system/src/Grav/Common/Config/CompiledBase.php @@ -13,7 +13,10 @@ use BadMethodCallException; use Exception; use RocketTheme\Toolbox\File\PhpFile; use RuntimeException; +use function filter_var; +use function function_exists; use function get_class; +use function ini_get; use function is_array; /** @@ -254,6 +257,9 @@ abstract class CompiledBase $file->save($cache); $file->unlock(); + + $this->preloadOpcodeCache($file); + $file->free(); $this->modified(); @@ -266,4 +272,40 @@ abstract class CompiledBase { return $this->object->toArray(); } + + /** + * Ensure compiled cache file is primed into OPcache when available. + */ + protected function preloadOpcodeCache(PhpFile $file): void + { + if (!function_exists('opcache_invalidate') || !$this->isOpcacheEnabled()) { + return; + } + + $filename = $file->filename(); + if (!$filename) { + return; + } + + // Silence errors for restricted functions while keeping best effort behavior. + @opcache_invalidate($filename, true); + + if (function_exists('opcache_compile_file')) { + @opcache_compile_file($filename); + } + } + + /** + * Detect if OPcache is active for current SAPI. + */ + protected function isOpcacheEnabled(): bool + { + $enabled = filter_var(ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN); + + if (PHP_SAPI === 'cli') { + $enabled = $enabled || filter_var(ini_get('opcache.enable_cli'), \FILTER_VALIDATE_BOOLEAN); + } + + return $enabled; + } }