smarty之缓存笔记

smarty缓存技术


  在smarty中,缓存分为:普通缓存,单模版都缓存,局部缓存。

缓存:
1:首选打开缓存配置项:$smarty->caching=true;

2:缓存生命周期的配置选项:$smarty->cache_lifetime=整秒数

3:$smarty->is_cached('index.html')判断index.html页面是否缓存,如果缓存,避免io操作

复制代码
 1 include('./libs/Smarty.class.php');
 2 $smarty=new Smarty();
 3 $conn=mysql_connect('127.0.0.1','root','');
 4 mysql_query('use smarty_cache',$conn);
 5 mysql_query('set nemes gbk',$conn);
 6 
 7 $smarty->caching=true;
 8 //开启缓存
 9 $id=$_GET['id']+0;
10 $sql='select * from user where id='.$id;
11 if(!$smarty->is_cached('index.html'))
12 {
13 $result=mysql_query($sql,$conn);
14 $row=mysql_fetch_assoc($result);
15     $smarty->assign('id',$row['id']);
16     $smarty->assign('name',$row['name']);
17     echo '走了数据库';
18 }
19 $smarty->display('index.html');
复制代码

 


单模版,多缓存

1:缓存参数$smarty->display('index.html',$id);
2:$smarty->is_cached('index.html',$id)判断index.html页面是否缓存,通过id来判断,id可以通过拼凑,可以参考ecshop中的缓存。
原则:凡是能影响页面内容的变量,就要根据此变量cached_id

复制代码
include('./libs/Smarty.class.php');
$smarty=new Smarty();
$conn=mysql_connect('127.0.0.1','root','');
mysql_query('use smarty_cache',$conn);
mysql_query('set nemes gbk',$conn);

$smarty->caching=true;
//开启缓存
$id=$_GET['id']+0;
$sql='select * from user where id='.$id;
$cached_id=$id+'XX'
if(!$smarty->is_cached('index.html',$cached_id))
{
$result=mysql_query($sql,$conn);
$row=mysql_fetch_assoc($result);
    $smarty->assign('id',$row['id']);
    $smarty->assign('name',$row['name']);
    echo '走了数据库';
}
$smarty->display('index.html',$cached_id);
复制代码

 



3:局部缓存:
在模版中不需要缓存的地方,用{insert name='nocachedid'}来表示,那么该处最终的值就是,insert_nocachedid()函数的返回值


 <body>
    这是这是一个smarty缓存技术的页面页面<br/>
    {insert name=getname}
    编号:{$id}&nbsp;姓名:{$name}

 </body>
复制代码
include('./libs/Smarty.class.php');
$smarty=new Smarty();
$conn=mysql_connect('127.0.0.1','root','');
mysql_query('use smarty_cache',$conn);
mysql_query('set nemes gbk',$conn);
function insert_getname()
{

    return $_SESSION['name']
}
$smarty->caching=true;
//开启缓存
$id=$_GET['id']+0;
$sql='select * from user where id='.$id;
if(!$smarty->is_cached('index.html',$id))
{
$result=mysql_query($sql,$conn);
$row=mysql_fetch_assoc($result);
    $smarty->assign('id',$row['id']);
    $smarty->assign('name',$row['name']);
    echo '走了数据库';
}
$smarty->display('index.html',$id);
复制代码

在ecshop中,用到了smarty模版技术,只能复习下smarty技术了

posted @   尼科  阅读(184)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示