1,關於文件的靜態緩存tp3有內置的F方法。
F方法他在公共函數庫文件function.php裏面
格式如下:
/**
* 快速文件數據讀取和保存 針對簡單類型數據 字符串、數組
* @param string $name 緩存名稱
* @param mixed $value 緩存值
* @param string $path 緩存路徑
* @return mixed
*/
function F($name, $value='', $path=DATA_PATH) {
static $_cache = array();
$filename = $path . $name . '.php';
if ('' !== $value) {
if (is_null($value)) {
// 刪除緩存
if(false !== strpos($name,'*')){
return false; // TODO
}else{
unset($_cache[$name]);
return Think\Storage::unlink($filename,'F');
}
} else {
Think\Storage::put($filename,serialize($value),'F');
// 緩存數據
$_cache[$name] = $value;
return null;
}
}
// 獲取緩存數據
if (isset($_cache[$name]))
return $_cache[$name];
if (Think\Storage::has($filename,'F')){
$value = unserialize(Think\Storage::read($filename,'F'));
$_cache[$name] = $value;
} else {
$value = false;
}
return $value;
}
後面文件名可不寫,則默認緩存到ThinkPHP的運行文件夾Rutime的文件夾Data裏面,可直接讀取緩存信息
F(‘site_config’,data是表單提交過來的數據
data,APP_PATH.‘Common/Conf/’);
$data = F(’…/…/Common/Conf/site_config’);
2,生成靜態頁面的buildHtml方法
buildHtml方法 在ThinkPHP框架文件夾controller.php文件裏
/**
* 創建靜態頁面
* @access protected
* @htmlfile 生成的靜態文件名稱
* @htmlpath 生成的靜態文件路徑
* @param string $templateFile 指定要調用的模板文件
* 默認爲空 由系統自動定位模板文件
* @return string
* protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='') {
*/
它的使用也極其簡單,以首頁生成靜態頁面爲例,有三個步驟:
(1),修改配置文件conf.php,開啓靜態生成文件後綴名爲.html
‘HTML_FILE_SUFFIX’ => ‘.html’,//設置靜態生成文件的文件名後綴爲html
(2),設置在index.php設置HTML_PATH,如
define(‘HTML_PATH’,’./’);//此爲生成首頁靜態頁面的根目錄
(3),gei首頁index方法加個參數且默認爲空,
index(type=''){
//寫在方法末尾00000
//當index方法參數爲buildHtml是執行生成靜態頁面
if(type=‘buildHtml’){
$this->buildHtml('index',HTML_PATH,'Index/index');
else {
$this->display();
}
}
//寫個方法用來在後臺手動控制生成靜態頁面
public function build_html() {
$this->index('buildHtml');
$this->ajaxReturn('OK');
}
3,生成靜態頁面還要解決用戶操作信息及時反饋,即計數器及時更新靜態頁面的信息
(1),寫前臺模板,導入計數器文件
<dd class="news-info">
{$vo.keywords} <span>{$vo.add_time|date="Y-m-d H:i:s",###}</span> 閱讀(<i news-id="{$vo.id}" class="news_count node-{$vo.id}">{$vo.count}</i>)
</dd>
<script src="__PUBLIC_HOME__/js/count.js"></script>
(2),計數器文件方法實現閱讀時更新,與後臺交互
/**
* 計數器JS文件
*/
var newsIds = {};
$(".news_count").each(function(i){
newsIds[i] = $(this).attr("news-id");
});
//調試
//console.log(newsIds);
url = "/index/getCount";
$.post(url, newsIds, function(result){
if(result.status == 1) {
counts = result.data;
$.each(counts, function(id,count){
$(".node-"+id).html(count);
});
}
}, "JSON");
(3),後臺操作方法
public function getCount() {
if(!$_POST) {
$this->ajaxReturn('沒有任何內容');
}
$newsIds = array_unique($_POST);
try{
$list = $this->getNewsByNewsIdIn($newsIds);
}catch (Exception $e) {
$this->ajaxReturn($e->getMessage());
}
if(!$list) {
$this->ajaxReturn('沒有數據');
}
$data = array();
foreach($list as $k=>$v) {
$data[$v['id']] = $v['count'];
}
$this->ajaxReturn(array('status'=>1,'data'=>$data));
}
4,定時執行任務生成靜態頁面和備份數據庫
就是通過腳本定時執行生成靜態頁面和備份數據庫的方法
(1),自動生成靜態頁面,定義常量標記是否爲計劃任務腳本,同樣在根目錄下的index.php入口文件中
define(‘APP_CRONTAB’, 1);
然後操作方法(寫前臺控制器那個從之前都行,比如index控制器)
public function crontab_build_html() {
if(APP_CRONTAB != 1) {//在index.php裏面,
die("the_file_must_exec_crontab");
}
$result = F('../../Common/Conf/site_config');
if(!$result['cacheindex']) {//後臺設置單選框name屬性值,如
**
<label><input type="radio" name="cacheindex" value="1" <if condition="$data.cacheindex eq 1">checked</if>> 開啓</label>
<label><input type="radio" name="cacheindex" value="0" <if condition="$data.cacheindex neq 1">checked</if>> 關閉</label>
*/
die('系統沒有設置開啓自動生成首頁緩存的內容');
}
$this->index('buildHtml');
}
(2),自動備份數據庫,定義常量標記是否爲計劃任務腳本,同樣在根目錄下的index.php入口文件中
新建個cron.php類執行數據庫備份任務
<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Upload;
/**
* 後臺計劃任務 業務腳本
*/
class CronController {
public function dumpmysql() {
$result = F('../../Common/Conf/site_config');
if(!$result['dumpmysql']) {//後臺設置單選框name屬性值,如
?**
<label><input type="radio" name="dumpmysql" value="1" <if condition="$data.dumpmysql eq 1">checked</if>> 開啓</label>
<label><input type="radio" name="dumpmysql" value="0" <if condition="$data.dumpmysql neq 1">checked</if>> 關閉</label>
*/
die("系統沒有設置開啓自動備份數據庫的內容");
}
$shell = "mysqldump -u".C("DB_USER")." " .C("DB_NAME")." > /tmp/cms".date("Ymd").".sql";
exec($shell);
}
}
我們可以用命令行的形式啓用這些定時任務,當然更簡便方法是用寶塔面板執行