首页 > php教程 > php手册 > 正文

php中smarty模板引擎中缓存的应用

php中文网
发布: 2016-06-06 19:55:32
原创
1404人浏览过

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 php中smarty模板引擎中缓存的应用: 1,Smarty缓存配置方法: $smarty-cache_dir=“目录名”; //创建缓存目录名 $smarty-caching=true; //开启缓存,为false的时候缓存无效 $smarty-cache_lifetime=60; /

欢迎进入linux社区论坛,与200万技术人员互动交流 >>进入

    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的内容如下:

   

    function smarty_block_cacheless($param, $content, &$smarty) {

    return $content;

    }

    ?>

    2) 编写程序及模板

    示例程序:testCacheLess.php

   

    include('Smarty.class.php');

    $smarty = new Smarty;

    $smarty->caching=true;

    $smarty->cache_lifetime = 6;

    $smarty->display('cache.tpl');

    ?>

    所用的模板:cache.tpl

    已经缓存的:{$smarty.now}

    {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来压缩和解压

php中smarty模板引擎中缓存的应用

相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号