smarty数据缓存与模板缓存
smarty会将用smarty语法编写的模板替换为php格式的以便PHP解析,实现PHP的数据与界面分离.
当我们每修改一次模板文件,相应的模板缓存都要重新生成一次.
但这仅仅是模板缓存,程序仍然需要从数据库获取数据及进行运算,与直接用PHP写出的界面无任何区别.
smarty支持真正的数据缓存,就是条件符合会直接给浏览器返回一个缓存过的静态文件,不会从数据库检索.
需添加如下设置:
$smarty->caching = true;//打开缓存 $smarty->cache_dir = '../cache/';//缓存目录,可自定
当我们运行程序时,我们会发现cache目录下生成一些html文件,打开可以看到都是静态HTML页面
insert函数默认是不缓存的.并且这个属性不能修改.
cache1.htm
{insert name="mytime"}
cache1.php
function insert_mytime(){
return date("Y-m-d H:i:s");
}
smarty_block函数也可以实现局部缓存
{blockname}
当前时间:{$smarty.now}
{/blockname}
$smarty->display('模板文件',缓存id); //创建带ID的缓存
$smarty->clear_all_cache(); //清除所有缓存
$smarty->clear_cache('模板文件');//清除指定模板文件的缓存
$smarty->clear_cache('模板文件',缓存id);//清除指定id的缓存
比如可以根据ID号生成不同的缓存页面:
$id="?id=".$_GET['id'];
$smarty->display("index.tpl",$id);
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号