From 01a3c5aee5c4c7d84077d911a9791014a038acab Mon Sep 17 00:00:00 2001 From: tale Date: Thu, 17 Mar 2016 23:29:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=86=85=E7=BD=AE=E6=A8=A1?= =?UTF-8?q?=E7=89=88=E5=BC=95=E6=93=8E=E6=97=A0=E6=B3=95=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=9D=99=E6=80=81=E7=BC=93=E5=AD=98=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Template.php | 8 ++++++++ tests/thinkphp/library/think/templateTest.php | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) 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__')); + } }