博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php smarty模版引擎中的缓存应用
阅读量:7044 次
发布时间:2019-06-28

本文共 1662 字,大约阅读时间需要 5 分钟。

php中smarty模版引擎中的缓存应用实现代码,需要的朋友可以参考下。
1,Smarty缓存的配置: 
代码如下:
$smarty->cache-dir="目录名"; //创建缓存目录名 
$smarty->caching=true; //开启缓存,为false的时候缓存无效 
$smarty->cache_lifetime=60; //缓存时间,单位是秒 
2,Smarty缓存的使用与清除 
代码如下:
$marty->display("cache.tpl",cache_id); //创建带ID的缓存 
$marty->clear_all_cache(); //清楚所有缓存 
$marty->clear_cache("index.php"); //清楚index.php中的缓存 
$marty->clear_cache("index.php',cache_id); //清楚index.php中指定ID的缓存 
3,Smarty的局部缓存 
第一个: insert_函数默认是不缓存,这个属性是不能修改 
使用方法:例子 
index.php中, 
function insert_get_time(){ 
return date("Y-m-d H:m:s"); 
index.html中, 
{insert name="get_time"} 
第二个: smarty_block 
定义一个block:smarty_block_name($params,$content, &$smarty){return $content;} //name表示区域名 
注册block:$smarty->register_block('name', 'smarty_block_name', false); //第三参数false表示该区域不被缓存 
模板写法:{name}内容{/name} 
写成block插件: 
1)定义一件插件函数:block.cacheless.php,放在smarty的plugins目录 
block.cacheless.php的内容如下: 
<?php 
function smarty_block_cacheless($param, $content, &$smarty) { 
return $content; 
?> 
2) 编写程序及模板 
示例程序:testCacheLess.php 
<?php 
include('Smarty.class.php'); 
$smarty = new Smarty; 
$smarty->caching=true; 
$smarty->cache_lifetime = 6; 
$smarty->display('cache.tpl'); 
?> 
所用的模板:cache.tpl 
已经缓存的:{$smarty.now}<br> 
{cacheless} 
没有缓存的:{$smarty.now} 
{/cacheless} 
4自定义缓存 
设置cache_handler_func使用自定义的函数处理缓存 
如: 
$smarty->cache_handler_func = "myCache"; 
function myCache($action, &$smarty_obj, &$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null){ 
该函数的一般是根椐$action来判断缓存当前操作: 
switch($action){ 
case "read"://读取缓存内容 
case "write"://写入缓存 
case "clear"://清空 
一般使用md5($tpl_file.$cache_id.$compile_id)作为唯一的cache_id 
如果需要,可使用gzcompress和gzuncompress来压缩和解压

转载于:https://www.cnblogs.com/feng12345/p/5474146.html

你可能感兴趣的文章
proftpd+ssl安装及使用
查看>>
利用自反ACL实现VLAN之间的单向访问
查看>>
mysql 修改表结构操作
查看>>
日志分析工具Awstats实战之Nginx篇-分析结果静态化
查看>>
【COCOS2DX-LUA 脚本开发之七】解决argument #2 is ‘xx’; ‘CCNode’ expected
查看>>
App5.0程序导入及发布
查看>>
使用@noescape解决Swift闭包“保留环”问题
查看>>
Unity加载模块深度解析(网格篇)
查看>>
主机安全(5)iptables状态检测
查看>>
Windows 7 服务优化讨论
查看>>
使用Gradle发布Android开源项目到JCenter
查看>>
分布式系统
查看>>
H3C路由器上配置本地端口镜像
查看>>
没有PowerPoint2010的环境如何播放.PPTX文件
查看>>
Outlook 2010 中查看邮件头
查看>>
AWS - Cloudfront CDN 测试
查看>>
PXE+Kickstart详细版 【第一次编辑】
查看>>
云平台与云主机选择的经验和建议
查看>>
如何验证及更改XenDesktop版本
查看>>
linux tc 对本机网卡限速
查看>>