diff --git a/library/think/Template.php b/library/think/Template.php index 31a258c9..10d14142 100644 --- a/library/think/Template.php +++ b/library/think/Template.php @@ -169,6 +169,14 @@ class Template if ($config) { $this->config($config); } + if (!empty($this->config['cache_id']) && $this->config['display_cache']) { + // 读取渲染缓存 + $cacheContent = Cache::get($this->config['cache_id']); + if ($cacheContent !== false) { + echo $cacheContent; + return; + } + } $template = $this->parseTemplateFile($template); if ($template) { $cacheFile = $this->config['cache_path'] . $this->config['cache_prefix'] . md5($template) . $this->config['cache_suffix']; diff --git a/tests/thinkphp/library/think/templateTest.php b/tests/thinkphp/library/think/templateTest.php index ceba98d5..3d4653f5 100644 --- a/tests/thinkphp/library/think/templateTest.php +++ b/tests/thinkphp/library/think/templateTest.php @@ -319,8 +319,10 @@ EOF; $template = new Template(); $template->assign('name', 'name'); $config = [ - 'strip_space' => true, - 'view_path' => dirname(__FILE__) . '/', + 'strip_space' => true, + 'view_path' => dirname(__FILE__) . '/', + 'cache_id' => '__CACHE_ID__', + 'display_cache'=> true ]; $data = ['name' => 'value']; $template->layout('layout')->display('display', $data, $config); @@ -401,4 +403,12 @@ EOF; $template->assign($data); $this->assertEquals($data, $template->get()); } + + public function testIsCache() + { + $template = new Template(['cache_id' => '__CACHE_ID__','display_cache' => true]); + $this->assertTrue(!$template->isCache('__CACHE_ID__')); + $template->display_cache = false; + $this->assertTrue(!$template->isCache('__CACHE_ID__')); + } }