From 1ccfca17d64d6a6b257408fb45776c4cb3547260 Mon Sep 17 00:00:00 2001 From: Stephan Lueckl Date: Wed, 14 Aug 2024 21:58:51 +0200 Subject: [PATCH] Fixing forced OpCache invalidation on every template include, which is resulting in fast raising wasted OpCache memory #1007 (#1047) * Fixing forced OpCache Invalidation on every call, which is resulting in fast raising wasted memory * Fix undefined $path variable warning --------- Co-authored-by: Daniel Metzner --- src/Resource/FilePlugin.php | 9 ++++++--- src/Template/Compiled.php | 21 ++++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/Resource/FilePlugin.php b/src/Resource/FilePlugin.php index 7fd8667d3..c59595783 100644 --- a/src/Resource/FilePlugin.php +++ b/src/Resource/FilePlugin.php @@ -56,11 +56,14 @@ public function populate(Source $source, Template $_template = null) { * @param Source $source source object */ public function populateTimestamp(Source $source) { - if (!$source->exists && $path = $this->getFilePath($source->name, $source->getSmarty(), $source->isConfig)) { - $source->timestamp = $source->exists = is_file($path); + $path = $this->getFilePath($source->name, $source->getSmarty(), $source->isConfig); + if (!$source->exists) { + $source->exists = ($path !== false && is_file($path)); } - if ($source->exists && $path) { + if ($source->exists && $path !== false) { $source->timestamp = filemtime($path); + } else { + $source->timestamp = 0; } } diff --git a/src/Template/Compiled.php b/src/Template/Compiled.php index caee87f32..5a07db0e6 100644 --- a/src/Template/Compiled.php +++ b/src/Template/Compiled.php @@ -136,7 +136,7 @@ private function compileAndLoad(Template $_smarty_tpl) { if ($this->exists && !$_smarty_tpl->getSmarty()->force_compile && !($_smarty_tpl->compile_check && $_smarty_tpl->getSource()->getTimeStamp() > $this->getTimeStamp()) ) { - $this->loadCompiledTemplate($_smarty_tpl); + $this->loadCompiledTemplate($_smarty_tpl, false); } if (!$this->isValid) { @@ -241,16 +241,19 @@ private function write(Template $_template, $code) { * HHVM requires a workaround because of a PHP incompatibility * * @param Template $_smarty_tpl do not change/remove variable name, is used by compiled template + * @param bool $invalidateCachedFiles forces a revalidation of the file in opcache or apc cache (if available) * */ - private function loadCompiledTemplate(Template $_smarty_tpl) { - - if (function_exists('opcache_invalidate') - && (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1) - ) { - opcache_invalidate($this->filepath, true); - } elseif (function_exists('apc_compile_file')) { - apc_compile_file($this->filepath); + private function loadCompiledTemplate(Template $_smarty_tpl, bool $invalidateCachedFiles = true) { + + if ($invalidateCachedFiles) { + if (function_exists('opcache_invalidate') + && (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1) + ) { + opcache_invalidate($this->filepath, true); + } elseif (function_exists('apc_compile_file')) { + apc_compile_file($this->filepath); + } } if (defined('HHVM_VERSION')) { eval('?>' . file_get_contents($this->filepath));