【php开源代码栏目提醒】:网学会员为广大网友收集整理了,viewCache.php,希望对大家有所帮助!
<?php
/*
Plugin Name: viewCache
Plugin URI: http://thinkphp.cn/
Description: 瑙嗗浘缂撳瓨鎻掍欢锛屾敮鎸佸叏閮ㄨ鍥惧拰灞閮ㄨ鍥剧紦瀛?
Author: 娴佸勾
Version: 1.0
Author URI: http://blog.liu21st.com/
*/
/**
+----------------------------------------------------------
* 妫鏌ュ苟璇诲彇瑙嗗浘缂撳瓨
*
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
function checkViewCache()
{
$cacheInfo = S(MODULE_NAME.'_'.ACTION_NAME.'_CACHE');
if($cacheType = $cacheInfo['type']) {
// 鍏ㄥ眬瑙嗗浘缂撳瓨鏃堕棿
$cacheTime = $cacheInfo['time'];
if($cacheType=='ALL') {
//鍏ㄩ儴瑙嗗浘缂撳瓨
$cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html';
if(file_exists($cacheFile) && time() <= filemtime($cacheFile)+$cacheTime) {
// 瑙嗗浘缂撳瓨鏈夋晥 璇诲彇缂撳瓨Action
readfile($cacheFile);
exit();
}
}elseif($cacheType=='PART') {
//灞閮ㄨ鍥剧紦瀛?
// 鑾峰彇瑙嗗浘缂撳瓨鏁版嵁
$view_cache = S(MODULE_NAME.'_'.ACTION_NAME.'_DATA');
if($view_cache) {
// 瀛樺湪瑙嗗浘缂撳瓨鏁版嵁
if($cacheInfo['default']=='NOCACHE') {
foreach($view_cache as $key=>$val) {
if(isset($val['expire'])) {
// 灞閮ㄨ鍥剧紦瀛樻椂闂?
$cacheTime = $val['expire'];
}
$cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'_'.$val['id'].'.html'; //缂撳瓨鏂囦欢瀹氫綅
if(file_exists($cacheFile) && time() <= filemtime($cacheFile)+$cacheTime) {
// 瑙嗗浘缂撳瓨鏈夋晥 缁橝ction璋冪敤
Session::set(MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'],true);
}else {
Session::set(MODULE_NAME.'_'.ACTION_NAME.'_'.$val['id'],null);
}
}
}
elseif($cacheInfo['default']=='CACHE') {
$cacheFile = TEMP_PATH.md5($_SERVER['REQUEST_URI']).'.html'; //缂撳瓨鏂囦欢瀹氫綅
if(file_exists($cacheFile) && time() <= filemtime($cacheFile)+$cacheTime) {
// 瑙嗗浘缂撳瓨鏈夋晥 缁橝ction璋冪敤
Session::set(MODULE_NAME.'_'.ACTION_NAME.'_CACHE',true);
}else {
Session::set(MODULE_NAME.'_'.ACTION_NAME.'_CACHE',null);
}
}
}
}
}
return ;
}
// 鏇挎崲妯$増缂撳瓨鏍囩 姣忔妯$増缂栬瘧鐨勬椂鍊欐墽琛?
function replaceCacheTag($content)
{
//鎼滅储鏄惁鏈塗hinkCache鏍囩 鍒ゆ柇瑙嗗浘缂撳瓨绫诲瀷 <thinkcache type="" time="" />
$find = preg_match('/<thinkcache\s(.+?)\s\/>\W/is',$content,$matches);
if($find) {
//鏇挎崲ThinkCache鏍囩
$content = str_replace($matches[0],'',$content);
//瑙f瀽ThinkCache鏍囩
$tagLibs = $matches[1];
$xml = '<tpl><tag '.$tagLibs.' /></tpl>';
$xml = simplexml_load_string($xml);
$xml = (array)($xml->tag->attributes());
$array = array_change_key_case($xml['@attributes']);
if(!isset($array['time'])) {
$array['time'] = C('
HTML_CACHE_TIME');
}
if(!isset($array['default'])) {
$array['default'] = 'nocache';
}
$cacheInfo = array_map('strtoupper',$array);
S(MODULE_NAME.'_'.ACTION_NAME.'_CACHE',$cacheInfo,-1);
}else
$cacheInfo = array('type'=>'', 'default'=>'');
if($cacheInfo['type']=='PART') {
// 閮ㄥ垎瑙嗗浘缂撳瓨 鍒嗕袱绉嶆儏鍐?
$cacheData = array();
if($cacheInfo['default'] == 'NOCACHE') {
// 榛樿閮ㄥ垎涓轰笉缂撳瓨 妫鏌ユā鐗堟槸鍚﹀瓨鍦?缂撳瓨鏍囩 <cache id="" expire=""></cache>
$find = preg_match_all('/<cache\s(.+?)>(.+?)<\/cache>/is',$content,$matches,PREG_SET_ORDER);
if($find) {
// 鑾峰彇瑙嗗浘缂撳瓨鏁版嵁
//瑙f瀽Cache鏍囩
foreach($matches as $key=>$match) {
// 鏇挎崲鍖归厤鐨勬爣绛?
$cacheAttr = $match[1];
$cacheContent = $match[2];
$xml = '<think><cache '.$cacheAttr.' /></think>';
$xml = simplexml_load_string($
xml);
$xml = (array)($xml->cache->attributes());
$array = array_change_key_case($xml['@attributes']);
$cacheId = $array['id'];
// 鐢熸垚缂撳瓨鏇挎崲鏍囩
$parseStr = '<cache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
$parseStr .= $cacheContent;
$parseStr .= '</cache_'.MODULE_NAME.'_'.ACTION_NAME.'_'.$cacheId.'>';
$content = str_replace($match[0],$parseStr,$content);
// 璁板綍瑙嗗浘缂撳瓨鏁版嵁
$cacheData[] = $array;
}
// 璁板綍鎵鏈夌殑瑙嗗浘缂撳瓨鏁版嵁
S(MODULE_NAME.'_'.ACTION_NAME.'_DATA',$cacheData,-1);
}
}elseif($cacheInfo['defa