修复模板更新后不更新缓存的问题

This commit is contained in:
oldrind
2016-03-12 18:18:46 +08:00
parent 2955e89f3d
commit 95fcc59bd5
3 changed files with 49 additions and 59 deletions

View File

@@ -170,6 +170,7 @@ class Template
$this->config($config); $this->config($config);
} }
$template = $this->parseTemplateFile($template); $template = $this->parseTemplateFile($template);
if ($template) {
$cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template) . $this->config['cache_suffix']; $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template) . $this->config['cache_suffix'];
if (!$this->checkCache($cacheFile)) { if (!$this->checkCache($cacheFile)) {
// 缓存无效 重新模板编译 // 缓存无效 重新模板编译
@@ -189,6 +190,7 @@ class Template
} }
echo $content; echo $content;
} }
}
/** /**
* 渲染模板内容 * 渲染模板内容
@@ -239,21 +241,24 @@ class Template
*/ */
private function checkCache($cacheFile) private function checkCache($cacheFile)
{ {
if (!$this->config['tpl_cache']) { if ($this->config['tpl_cache'] && is_file($cacheFile) && $handle = @fopen($cacheFile, "r")) {
// 优先对配置设定检测 // 读取第一行
return false;
}
if (is_file($cacheFile)) {
$handle = @fopen($cacheFile, "r");
if ($handle) {
preg_match('/\/\*(.+?)\*\//', fgets($handle), $matches); preg_match('/\/\*(.+?)\*\//', fgets($handle), $matches);
if (isset($matches[1])) { if (isset($matches[1])) {
$this->includeFile = unserialize($matches[1]); $includeFile = unserialize($matches[1]);
// 检查编译存储是否有效 if (is_array($includeFile)) {
return $this->storage->check($this->includeFile, $cacheFile, $this->config['cache_time']); // 检查模板文件是否有更新
foreach($includeFile as $path => $time) {
if (is_file($path) && filemtime($path) > $time) {
// 模板文件如果有更新则缓存需要更新
return false;
} }
} }
} }
// 检查编译存储是否有效
return $this->storage->check($cacheFile, $this->config['cache_time']);
}
}
return false; return false;
} }
@@ -289,14 +294,9 @@ class Template
} else { } else {
// 读取布局模板 // 读取布局模板
$layoutFile = $this->parseTemplateFile($this->config['layout_name']); $layoutFile = $this->parseTemplateFile($this->config['layout_name']);
// 检查布局文件 if ($layoutFile) {
if (is_file($layoutFile)) {
// 替换布局的主体内容 // 替换布局的主体内容
$content = str_replace($this->config['layout_item'], $content, file_get_contents($layoutFile)); $content = str_replace($this->config['layout_item'], $content, file_get_contents($layoutFile));
// 记录布局文件的更新时间
$this->includeFile[filemtime($layoutFile)] = $layoutFile;
} else {
throw new Exception('template not exist:' . $layoutFile, 10700);
} }
} }
} }
@@ -417,12 +417,10 @@ class Template
if (!$this->config['layout_on'] || $this->config['layout_name'] != $array['name']) { if (!$this->config['layout_on'] || $this->config['layout_name'] != $array['name']) {
// 读取布局模板 // 读取布局模板
$layoutFile = $this->parseTemplateFile($array['name']); $layoutFile = $this->parseTemplateFile($array['name']);
if (is_file($layoutFile)) { if ($layoutFile) {
$replace = isset($array['replace']) ? $array['replace'] : $this->config['layout_item']; $replace = isset($array['replace']) ? $array['replace'] : $this->config['layout_item'];
// 替换布局的主体内容 // 替换布局的主体内容
$content = str_replace($replace, $content, file_get_contents($layoutFile)); $content = str_replace($replace, $content, file_get_contents($layoutFile));
// 记录布局文件的更新时间
$this->includeFile[filemtime($layoutFile)] = $layoutFile;
} }
} }
} else { } else {
@@ -996,11 +994,9 @@ class Template
$templateName = $this->get(substr($templateName, 1)); $templateName = $this->get(substr($templateName, 1));
} }
$template = $this->parseTemplateFile($templateName); $template = $this->parseTemplateFile($templateName);
if (is_file($template)) { if ($template) {
// 获取模板文件内容 // 获取模板文件内容
$parseStr .= file_get_contents($template); $parseStr .= file_get_contents($template);
// 记录模板文件的更新时间
$this->includeFile[filemtime($template)] = $template;
} }
} }
return $parseStr; return $parseStr;
@@ -1010,7 +1006,7 @@ class Template
* 解析模板文件名 * 解析模板文件名
* @access private * @access private
* @param string $template 文件名 * @param string $template 文件名
* @return string * @return string|false
*/ */
private function parseTemplateFile($template) private function parseTemplateFile($template)
{ {
@@ -1020,7 +1016,14 @@ class Template
APP_PATH . str_replace('@', '/' . basename($this->config['view_path']) . '/', $template) . $this->config['view_suffix'] : APP_PATH . str_replace('@', '/' . basename($this->config['view_path']) . '/', $template) . $this->config['view_suffix'] :
(defined('THEME_PATH') && substr_count($template, '/') < 2 ? THEME_PATH : $this->config['view_path']) . $template . $this->config['view_suffix']; (defined('THEME_PATH') && substr_count($template, '/') < 2 ? THEME_PATH : $this->config['view_path']) . $template . $this->config['view_suffix'];
} }
if (is_file($template)) {
// 记录模板文件的更新时间
$this->includeFile[$template] = filemtime($template);
return $template; return $template;
} else {
throw new Exception('template not exist:' . $template, 10700);
return false;
}
} }
/** /**

View File

@@ -52,19 +52,13 @@ class File
/** /**
* 检查编译缓存是否有效 * 检查编译缓存是否有效
* @array $templates 用到的模板更新时间列表 * @array $templates 用到的模板文件及更新时间列表
* @string $cacheFile 缓存的文件名 * @string $cacheFile 缓存的文件名
* @int $cacheTime 缓存时间 * @int $cacheTime 缓存时间
* @return boolean * @return boolean
*/ */
public function check($templates, $cacheFile, $cacheTime) public function check($cacheFile, $cacheTime)
{ {
foreach($templates as $time => $path) {
if (is_file($path) && filemtime($path) > $time) {
// 模板文件如果有更新则缓存需要更新
return false;
}
}
if (0 != $cacheTime && time() > filemtime($cacheFile) + $cacheTime) { if (0 != $cacheTime && time() > filemtime($cacheFile) + $cacheTime) {
// 缓存是否在有效期 // 缓存是否在有效期
return false; return false;

View File

@@ -69,19 +69,12 @@ class Sae
/** /**
* 检查编译缓存是否有效 * 检查编译缓存是否有效
* @array $templates 用到的模板更新时间列表
* @string $cacheFile 缓存的文件名 * @string $cacheFile 缓存的文件名
* @int $cacheTime 缓存时间 * @int $cacheTime 缓存时间
* @return boolean * @return boolean
*/ */
public function check($templates, $cacheFile, $cacheTime) public function check($cacheFile, $cacheTime)
{ {
foreach($templates as $time => $path) {
if (is_file($path) && filemtime($path) > $time) {
// 模板文件如果有更新则缓存需要更新
return false;
}
}
$mtime = $this->get($cacheFile, 'mtime'); $mtime = $this->get($cacheFile, 'mtime');
if (0 != $cacheTime && time() > $mtime + $cacheTime) { if (0 != $cacheTime && time() > $mtime + $cacheTime) {
// 缓存是否在有效期 // 缓存是否在有效期