Skip to content

Commit

Permalink
Fixing forced OpCache invalidation on every template include, which i…
Browse files Browse the repository at this point in the history
…s 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 <daniel.metzner@niceshops.com>
  • Loading branch information
stephanlueckl and dmetzner authored Aug 14, 2024
1 parent d6153d4 commit 1ccfca1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
9 changes: 6 additions & 3 deletions src/Resource/FilePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/Template/Compiled.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 1ccfca1

Please sign in to comment.