Files
framework/library/think/template/driver/Sae.php
oldrind 71dbdcb874 优代部分代码;block标签内可以使用{__block__}来引用所继承模板中相应block标签的内容;
模板包含文件记录及更新时间直接写入缓存的模板中;改进标签别名的处理方式,别名不再定义方法;
去除include标签传参自动生成变量的代码;cx类增加function方法,用来生成匿名函数,结合{~$函数名()}可用于递归的实现。
2016-03-11 21:58:18 +08:00

117 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\template\driver;
use think\Exception;
class Sae
{
// mc 对象
private $mc;
// 编译缓存内容
private $contents = [];
/**
* 架构函数
* @access public
*/
public function __construct()
{
if (!function_exists('memcache_init')) {
throw new Exception('请在SAE平台上运行代码。');
}
$this->mc = @memcache_init();
if (!$this->mc) {
throw new Exception('您未开通Memcache服务请在SAE管理平台初始化Memcache服务');
}
}
/**
* 写入编译缓存
* @string $cacheFile 缓存的文件名
* @string $content 缓存的内容
* @return void|array
*/
public function write($cacheFile, $content)
{
// 添加写入时间
$content = time() . $content;
if (!$this->mc->set($cacheFile, $content, MEMCACHE_COMPRESSED, 0)) {
throw new Exception('sae mc write error :' . $cacheFile);
} else {
$this->contents[$cacheFile] = $content;
return true;
}
}
/**
* 读取编译编译
* @string $cacheFile 缓存的文件名
* @array $vars 变量数组
* @return void
*/
public function read($cacheFile, $vars = [])
{
if (!empty($vars) && is_array($vars)) {
extract($vars, EXTR_OVERWRITE);
}
eval('?>' . $this->get($cacheFile, 'content'));
}
/**
* 检查编译缓存是否有效
* @array $templates 用到的模板更新时间列表
* @string $cacheFile 缓存的文件名
* @int $cacheTime 缓存时间
* @return boolean
*/
public function check($templates, $cacheFile, $cacheTime)
{
$mtime = $this->get($cacheFile, 'mtime');
if (0 != $cacheTime && time() > $mtime + $cacheTime) {
// 缓存是否在有效期
return false;
}
foreach($templates as $time => $path) {
if (is_file($path) && filemtime($path) > $time + $cacheTime) {
// 模板文件如果有更新则缓存需要更新
return false;
}
}
return true;
}
/**
* 读取文件信息
* @access private
* @param string $filename 文件名
* @param string $name 信息名 mtime或者content
* @return boolean
*/
private function get($filename, $name)
{
if (!isset($this->contents[$filename])) {
$this->contents[$filename] = $this->mc->get($filename);
}
$content = $this->contents[$filename];
if (false === $content) {
return false;
}
$info = array(
'mtime' => substr($content, 0, 10),
'content' => substr($content, 10),
);
return $info[$name];
}
}