时间:2021-07-01 10:21:17 帮助过:29人阅读
/**
* 处理模板文件
*
* @access public
* @param string $filename
* @param sting $cache_id
*
* @return sring
*/
function fetch($filename, $cache_id = '')
{
if (!$this->_seterror)
{
error_reporting(E_ALL ^ E_NOTICE);
}
$this->_seterror++;
if (strncmp($filename,'str:', 4) == 0)
{
$out = $this->_eval($this->fetch_str(substr($filename, 4)));
}
else
{
if ($this->_checkfile)
{
if (!file_exists($filename))
{
$filename = $this->template_dir . '/' . $filename;
}
}
else
{
$filename = $this->template_dir . '/' . $filename;
}
if ($this->direct_output)
{
$this->_current_file = $filename;
$out = $this->_eval($this->fetch_str(file_get_contents($filename)));
}
else
{
if ($cache_id && $this->caching)
{
$out = $this->template_out;
}
else
{
if (!in_array($filename, $this->template))
{
$this->template[] = $filename;
}
$out = $this->make_compiled($filename);
if ($cache_id)
{
$cachename = basename($filename, strrchr($filename, '.')) . '_' . $cache_id;
$data = serialize(array('template' => $this->template, 'expires' => $this->_nowtime + $this->cache_lifetime, 'maketime' => $this->_nowtime));
$out = str_replace("\r", '', $out);
$memcache = new Memcache;
$memcache->connect("127.0.0.1", 11211);
$CacheID = md5($filename.$cache_id.$cachename);
while (strpos($out, "\n\n") !== false)
{
$out = str_replace("\n\n", "\n", $out);
}
$memcache->set($CacheID , '' . $data . $out , 3600);
$this->template = array();
}
}
}
}
$this->_seterror--;
if (!$this->_seterror)
{
error_reporting($this->_errorlevel);
}
return $out; // 返回html数据
}
/**
* 判断是否缓存
*
* @access public
* @param string $filename
* @param sting $cache_id
*
* @return bool
*/
function is_cached($filename, $cache_id = '')
{
$cachename = basename($filename, strrchr($filename, '.')) . '_' . $cache_id;
$memcache = new Memcache;
$memcache->connect("127.0.0.1", 11211);
$CacheID = md5($filename.$cache_id.$cachename);
if ($this->caching == true && $this->direct_output == false)
{
if ($data = $memcache->get($CacheID))
{
$data = substr($data, 13);
$pos = strpos($data, '<');
$paradata = substr($data, 0, $pos);
$para = @unserialize($paradata);
if ($para === false || $this->_nowtime > $para['expires'])
{
$this->caching = false;
return false;
}
$this->_expires = $para['expires'];
$this->template_out = substr($data, $pos);
foreach ($para['template'] AS $val)
{
$stat = @stat($val);
if ($para['maketime'] < $stat['mtime'])
{
$this->caching = false;
return false;
}
}
}
else
{
$this->caching = false;
return false;
}
return true;
}
else
{
return false;
}
}