设为首页 收藏本站
查看: 776|回复: 0

[经验分享] php学习笔记(三十九)smarty缓存特性的使用(包括局部缓存)

[复制链接]

尚未签到

发表于 2017-4-13 09:36:07 | 显示全部楼层 |阅读模式
  

  init.smarty.php

<?php
define("ROOT", ".");
//解决问题:Warning: strftime() [function.strftime]:
date_default_timezone_set("Asia/Shanghai");
include ROOT."/libs/Smarty.class.php";
$tpl = new Smarty();
//smarty初始化
$tpl->template_dir=ROOT."/templates/";
$tpl->compile_dir=ROOT."/templates_c/";
//配置文件位置
$tpl->config_dir=ROOT."/configs/";
$tpl->caching=0;//缓存文件位置;开发阶段不要开启,运行阶段再开启
$tpl->cache_dir = ROOT."/cache/"; //设置缓存目录
$tpl->cache_lifetime=60;
$tpl->left_delimiter="<!--{";
$tpl->right_delimiter="}-->";
?>
  mysmarty.php

<?php
/**
*
* 以下所有使用变量都要基于设定的前缀和后缀<!--{}-->
* smarty的缓存类似于网页静态化(使用smarty缓存非常方便)
* 1.需要开启缓存
* 2.指定一下缓存的时间
* 3.指定缓存文件的保存位置
* 4.清除缓存
* $tpl->clear_cache["模板","cacheID"]
* $tpl->clear_all_cache();
*
*
* 注意:
* 1.一个模板只能有一个缓存文件
* 2.如果一个模板的多个文章,则需要每个文章进行缓存
* $tpl->display("mysmarty.html",cacheid);
* cacheid最好使用:$_SERVER["REQUEST_URI"]地址栏来进行存储
* 3.一定要使用smarty函数isCached来判断是否存在缓存
* 4.将用户你好,和发表评论时不进行缓存的设置(局部不缓存)
* 将数据写在非缓存之外
* 方法一:php注册方式
* $tpl->registerPlugin("block", "nocache", "ncache",FALSE);
* 注册块函数
* function ncache($param,$content){
* return $content;
* }
* 方法二:写块文件
* //注册块需要修改Smarty的编译文件
* //1.添加块文件
* //2.修改smartyCompile文件(700行左右)
* //if($tag_command=="nocache")
* //$this->_plugins['block'][$tag_command] = array($plugin_func,null,null,null,false);
* //else
* //$this->_plugins['block'][$tag_command] = array($plugin_func,null,null,null,true);
*/
//如果文件加载失败require会停止继续解析php;而include则会继续向下执行
require 'init.smarty.php';
require 'page.class.php';
//判断是否存在缓存文件
if(!$tpl->isCached("mysmarty.html",$_SERVER["REQUEST_URI"])){
$title="这是一个文字标题";
$mysqli = new mysqli("localhost","root","root","hibernate");
$result = $mysqli->query("select id,name,price from users");
$page = new Page($result->num_rows,5);
$result = $mysqli->query("select id,name,price from users {$page->limit}");
$data=array();
while ($row=$result->fetch_assoc()){
$data[]=$row;
}
$tpl->assign("title",$title);
$tpl->assign("secT",$data);
$tpl->assign("fpage",$page->fpage());
echo date("Y-m-d H:i:s");
}
//设置为局部缓存
$tpl->assign("date",date("Y-m-d H:i:s"));
$tpl->registerPlugin("block", "nocache", "ncache",FALSE);
/**
* 注册块函数
* @param unknown_type $param
* @param unknown_type $content
*/
function ncache($param,$content){
return $content;
}
//模板文件名可以随便定义:比如:mysmarty.tpl只有内容是html就可以了
//第二个参数,每变化一个值就存一份不同的缓存
$tpl->display("mysmarty.html",$_SERVER["REQUEST_URI"]);
?>
  page.class.php

<?php
class Page {
private $total; //数据表中总记录数
private $listRows; //每页显示行数
private $limit;
private $uri;
private $pageNum; //页数
private $config=array('header'=>"records", "prev"=>"prev", "next"=>"next", "first"=>"first", "last"=>"last");
private $listNum=8;
/*
* $total
* $listRows
*/
public function __construct($total, $listRows=10, $pa=""){
$this->total=$total;
$this->listRows=$listRows;
$this->uri=$this->getUri($pa);
$this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;
$this->pageNum=ceil($this->total/$this->listRows);
$this->limit=$this->setLimit();
}
private function setLimit(){
return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}";
}
private function getUri($pa){
$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa;
$parse=parse_url($url);
if(isset($parse["query"])){
parse_str($parse['query'],$params);
unset($params["page"]);
$url=$parse['path'].'?'.http_build_query($params);
}
return $url;
}
public function __get($args){
if($args=="limit")
return $this->limit;
else
return null;
}
private function start(){
if($this->total==0)
return 0;
else
return ($this->page-1)*$this->listRows+1;
}
private function end(){
return min($this->page*$this->listRows,$this->total);
}
private function first(){
if($this->page==1)
@$html.='';
else
@$html.="<a href='{$this->uri}&page=1'>{$this->config["first"]}</a>";
return $html;
}
private function prev(){
if($this->page==1)
@$html.='';
else
@$html.="<a href='{$this->uri}&page=".($this->page-1)."'>{$this->config["prev"]}</a>";
return $html;
}
private function pageList(){
$linkPage="";
$inum=floor($this->listNum/2);
for($i=$inum; $i>=1; $i--){
$page=$this->page-$i;
if($page<1)
continue;
$linkPage.="<a href='{$this->uri}&page={$page}'>{$page}</a>";
}
$linkPage.="{$this->page}";
for($i=1; $i<=$inum; $i++){
$page=$this->page+$i;
if($page<=$this->pageNum)
$linkPage.="<a href='{$this->uri}&page={$page}'>{$page}</a>";
else
break;
}
return $linkPage;
}
private function next(){
if($this->page==$this->pageNum)
@$html.='';
else
@$html.="<a href='{$this->uri}&page=".($this->page+1)."'>{$this->config["next"]}</a>";
return $html;
}
private function last(){
if($this->page==$this->pageNum)
@$html.='';
else
@$html.="<a href='{$this->uri}&page=".($this->pageNum)."'>{$this->config["last"]}</a>";
return $html;
}
private function goPage(){
return '<input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" >';
}
function fpage($display=array(0,1,2,3,4,5,6,7,8)){
$html[0]="total:<b>{$this->total}</b>{$this->config["header"]}";
$html[1]="pageSize:<b>".($this->end()-$this->start()+1)."</b>,currentSize:<b>{$this->start()}-{$this->end()}</b>";
$html[2]="<b>{$this->page}/{$this->pageNum}</b>";
$html[3]=$this->first();
$html[4]=$this->prev();
$html[5]=$this->pageList();
$html[6]=$this->next();
$html[7]=$this->last();
$html[8]=$this->goPage();
$fpage='';
foreach($display as $index){
$fpage.=$html[$index];
}
return $fpage;
}
}



  mysmarty.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><!--{$title}--></title>
</head>
<body>
<!--{nocache}-->
<!--{$date}-->
<!--{/nocache}-->
<tableborder="1" width="800">
<tr>
<th>行号</th>
<th>索引</th>
<th>ID</th>
<th>名称</th>
<th>价格</th>
</tr>
<!--{section loop=$secT name="ls"}-->
<tr>
<td><!--{$smarty.section.ls.rownum}--></td>
<td><!--{$smarty.section.ls.index}--></td>
<td><!--{$secT[ls].id}--></td>
<td><!--{$secT[ls].name}--></td>
<td><!--{$secT[ls].price}--></td>
</tr>
<!--{sectionelse}-->
<tr><td>数组为空,或没有分配</td></tr>
<!--{/section}-->
<tr>
<td colspan="5"><!--{$fpage}--></td>
</tr>
</table>
<br>
</body>
</html>


  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-364246-1-1.html 上篇帖子: 【捷哥浅谈PHP】第五弹 --- 分页之九阳神功 下篇帖子: PHP ORM框架与简单代码实现——让OOP与关系数据库更融洽
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表