From 8cbe51a6038269d3319f1664641135f009f33fba Mon Sep 17 00:00:00 2001 From: Uwe Tews Date: Mon, 20 Jul 2015 04:01:43 +0200 Subject: [PATCH] - improvement invalidate PHP 5.5 opcache for recompiled and cached templates https://github.com/smarty-php/smarty/issues/72 --- change_log.txt | 1 + libs/Smarty.class.php | 2 +- .../smarty_internal_cacheresource_file.php | 117 ++---------------- libs/sysplugins/smarty_template_compiled.php | 30 +++-- 4 files changed, 32 insertions(+), 118 deletions(-) diff --git a/change_log.txt b/change_log.txt index 52d1eaf9..505c2c63 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,6 +1,7 @@  ===== 3.1.28-dev===== (xx.xx.2015) 20.07.2015 - bugfix config files got recompiled on each request + - improvement invalidate PHP 5.5 opcache for recompiled and cached templates https://github.com/smarty-php/smarty/issues/72 12.07.2015 - optimize {extends} compilation diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 375e6d45..847b947f 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/29'; + const SMARTY_VERSION = '3.1.28-dev/30'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_cacheresource_file.php b/libs/sysplugins/smarty_internal_cacheresource_file.php index 0792fbe3..d0ab4ec5 100644 --- a/libs/sysplugins/smarty_internal_cacheresource_file.php +++ b/libs/sysplugins/smarty_internal_cacheresource_file.php @@ -33,10 +33,8 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource $_filepath = $_template->source->uid; // if use_sub_dirs, break file into directories if ($_template->smarty->use_sub_dirs) { - $_filepath = substr($_filepath, 0, 2) . DS - . substr($_filepath, 2, 2) . DS - . substr($_filepath, 4, 2) . DS - . $_filepath; + $_filepath = substr($_filepath, 0, 2) . DS . substr($_filepath, 2, 2) . DS . substr($_filepath, 4, 2) . DS . + $_filepath; } $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^'; if (isset($_cache_id)) { @@ -60,7 +58,8 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource } $cached->lock_id = $_lock_dir . sha1($_cache_id . $_compile_id . $_template->source->uid) . '.lock'; } - $cached->filepath = $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php'; + $cached->filepath = $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . + '.php'; $cached->timestamp = $cached->exists = is_file($cached->filepath); if ($cached->exists) { $cached->timestamp = filemtime($cached->filepath); @@ -88,7 +87,7 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource * @param Smarty_Internal_Template $_template template object * @param Smarty_Template_Cached $cached cached object * - * @return booleantrue or false if the cached content does not exist + * @return boolean true or false if the cached content does not exist */ public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null) { @@ -112,6 +111,9 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource { $obj = new Smarty_Internal_Write_File(); if ($obj->writeFile($_template->cached->filepath, $content, $_template->smarty) === true) { + if (function_exists('opcache_invalidate')) { + opcache_invalidate($_template->cached->filepath); + } $cached = $_template->cached; $cached->timestamp = $cached->exists = is_file($cached->filepath); if ($cached->exists) { @@ -163,108 +165,7 @@ class Smarty_Internal_CacheResource_File extends Smarty_CacheResource */ public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time) { - $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null; - $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null; - $_dir_sep = $smarty->use_sub_dirs ? '/' : '^'; - $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0; - $_dir = $smarty->getCacheDir(); - if ($_dir == '/') { //We should never want to delete this! - return 0; - } - $_dir_length = strlen($_dir); - if (isset($_cache_id)) { - $_cache_id_parts = explode('|', $_cache_id); - $_cache_id_parts_count = count($_cache_id_parts); - if ($smarty->use_sub_dirs) { - foreach ($_cache_id_parts as $id_part) { - $_dir .= $id_part . DS; - } - } - } - if (isset($resource_name)) { - $_save_stat = $smarty->caching; - $smarty->caching = true; - $tpl = new $smarty->template_class($resource_name, $smarty); - $smarty->caching = $_save_stat; - - // remove from template cache - $tpl->source; // have the template registered before unset() - if ($smarty->allow_ambiguous_resources) { - $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id; - } else { - $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id; - } - if (isset($_templateId[150])) { - $_templateId = sha1($_templateId); - } - unset($smarty->template_objects[$_templateId]); - - if ($tpl->source->exists) { - $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath)); - } else { - return 0; - } - } - $_count = 0; - $_time = time(); - if (file_exists($_dir)) { - $_cacheDirs = new RecursiveDirectoryIterator($_dir); - $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST); - foreach ($_cache as $_file) { - if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) { - continue; - } - // directory ? - if ($_file->isDir()) { - if (!$_cache->isDot()) { - // delete folder if empty - @rmdir($_file->getPathname()); - } - } else { - $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string) $_file, $_dir_length))); - $_parts_count = count($_parts); - // check name - if (isset($resource_name)) { - if ($_parts[$_parts_count - 1] != $_resourcename_parts) { - continue; - } - } - // check compile id - if (isset($_compile_id) && (!isset($_parts[$_parts_count - 2 - $_compile_id_offset]) || $_parts[$_parts_count - 2 - $_compile_id_offset] != $_compile_id)) { - continue; - } - // check cache id - if (isset($_cache_id)) { - // count of cache id parts - $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset; - if ($_parts_count < $_cache_id_parts_count) { - continue; - } - for ($i = 0; $i < $_cache_id_parts_count; $i ++) { - if ($_parts[$i] != $_cache_id_parts[$i]) { - continue 2; - } - } - } - // expired ? - if (isset($exp_time)) { - if ($exp_time < 0) { - preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_file), $match); - if ($_time < (@filemtime($_file) + $match[1])) { - continue; - } - } else { - if ($_time - @filemtime($_file) < $exp_time) { - continue; - } - } - } - $_count += @unlink((string) $_file) ? 1 : 0; - } - } - } - - return $_count; + return Smarty_Internal_Extension_Clear::clear($smarty, $resource_name, $cache_id, $compile_id, $exp_time); } /** diff --git a/libs/sysplugins/smarty_template_compiled.php b/libs/sysplugins/smarty_template_compiled.php index 0103fed8..e0e5dbcb 100644 --- a/libs/sysplugins/smarty_template_compiled.php +++ b/libs/sysplugins/smarty_template_compiled.php @@ -38,6 +38,7 @@ class Smarty_Template_Compiled * @var boolean */ public $processed = false; + /** * Code of recompiled template resource * @@ -99,18 +100,18 @@ class Smarty_Template_Compiled { $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null; if ($_template->source->isConfig) { - $_flag = '_' . ((int) $_template->smarty->config_read_hidden + (int) $_template->smarty->config_booleanize * 2 - + (int) $_template->smarty->config_overwrite * 4); + $_flag = '_' . + ((int) $_template->smarty->config_read_hidden + (int) $_template->smarty->config_booleanize * 2 + + (int) $_template->smarty->config_overwrite * 4); } else { - $_flag = '_' . ((int) $_template->smarty->merge_compiled_includes + (int) $_template->smarty->escape_html * 2); + $_flag = '_' . + ((int) $_template->smarty->merge_compiled_includes + (int) $_template->smarty->escape_html * 2); } $_filepath = $_template->source->uid . $_flag; // if use_sub_dirs, break file into directories if ($_template->smarty->use_sub_dirs) { - $_filepath = substr($_filepath, 0, 2) . DS - . substr($_filepath, 2, 2) . DS - . substr($_filepath, 4, 2) . DS - . $_filepath; + $_filepath = substr($_filepath, 0, 2) . DS . substr($_filepath, 2, 2) . DS . substr($_filepath, 4, 2) . DS . + $_filepath; } $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^'; if (isset($_compile_id)) { @@ -150,7 +151,10 @@ class Smarty_Template_Compiled public function process(Smarty_Internal_Template $_template) { $_smarty_tpl = $_template; - if ($_template->source->recompiled || !$_template->compiled->exists || $_template->smarty->force_compile || ($_template->smarty->compile_check && $_template->source->getTimeStamp() > $_template->compiled->getTimeStamp())) { + if ($_template->source->recompiled || !$_template->compiled->exists || $_template->smarty->force_compile || + ($_template->smarty->compile_check && + $_template->source->getTimeStamp() > $_template->compiled->getTimeStamp()) + ) { $this->compileTemplateSource($_template); $compileCheck = $_template->smarty->compile_check; $_template->smarty->compile_check = false; @@ -169,6 +173,9 @@ class Smarty_Template_Compiled ob_get_clean(); $this->code = null; } else { + if (function_exists('opcache_invalidate')) { + opcache_invalidate($_template->compiled->filepath); + } include($_template->compiled->filepath); } $_template->smarty->compile_check = $compileCheck; @@ -178,6 +185,9 @@ class Smarty_Template_Compiled $this->compileTemplateSource($_template); $compileCheck = $_template->smarty->compile_check; $_template->smarty->compile_check = false; + if (function_exists('opcache_invalidate')) { + opcache_invalidate($_template->compiled->filepath); + } include($_template->compiled->filepath); $_template->smarty->compile_check = $compileCheck; } @@ -288,12 +298,14 @@ class Smarty_Template_Compiled } return isset($this->content) ? $this->content : false; } + /** * Get compiled time stamp * * @return int */ - public function getTimeStamp() { + public function getTimeStamp() + { if ($this->exists && !isset($this->timestamp)) { $this->timestamp = @filemtime($this->filepath); }