zoukankan      html  css  js  c++  java
  • Smarty 模板引擎下缓存设置

    缓存:就是将用户重复使用的内容进行缓存生成HTML内容。

      缓存类型

        全局缓存: 将整个页面内容都生成静态内容。

          Cache_dir: 缓存目录配置

          Cache_lifetime:缓存的有效周期

          Cacheing:设置是否开启缓存

        局部缓存: 控制整个页面中的某个内容缓存,或者某个内容不缓存。

          insert函数调用的内容不缓存:

            定义insert类型的函数:

              Smarty 2.x版本:

                Function insert_插件名(参数列表){

                  功能代码

                }

              Smarty 3.x版本:

                Function smarty_insert_插件名(参数){

                  功能代码

                }

          前台调用

            {ts:insert name="插件名"}

        register_plugins()

          函数定义:

            Function smarty_modifier/block/function_插件名(参数){

              代码

            }

          动态注册函数为临时插件

            $tpl -> register_plugins("类型",函数名,前台调用的标签名)

            类型:modifier/block/function

            $tpl ->registerplugin("function","getmessage","samrty_function_插件名",控制是否缓存)

            True|默认不写:缓存数据

            False:不缓存数据

          前台调用:

            {ts:前台调用的标签名}

      【smarty.inc.php】 #首先要在smarty.inc.php中加入缓存文件的开启#

    <?php
    require_once("./libs/Smarty.class.php");
    $tpl = new Smarty;
    $tpl -> template_dir = "./templates/";
    $tpl -> compile_dir = "./compilers/";
    $tpl -> cache_dir = "./caches/";  //设置缓存文件目录
    $tpl -> cache_lifetime = 30;  //缓存文件保留时间
    $tpl -> caching = true;
    $tpl -> left_delimiter = "{ts:";
    $tpl -> right_delimiter= "}";
    ?>

      【contents.php】

    <?php
    require_once("./smarty.inc.php");
    require_once("./mysql.class.php");
    $id = 1;
    if(isset($_GET['id']))
        $id = $_GET['id'];
    
    $sql = "select * from ts_ch_news where id={$id}";
    $db = new MySql;
    $db -> query($sql);
    $data = $db -> find();
    $tpl -> assign("title",$data['title']);
    $tpl -> assign("body",$data['body']);
    
    $cacheid = md5($_SERVER['REQUEST_URI']);
    $tpl -> display("contents.html",$cacheid);
    ?>

      【contents.html】

    <html>
        <head>
            <title>{ts:$title}</title>
        </head>
        <body>
            {ts:$body}
        </body>
    </html>

      Display(模板文件名,缓存id);

        缓存id:文件的url地址进行md5加密

        $cache_id= md5($_SERVER['REQUEST_URI'])  

        #因为给缓存文件传值的时候需要id唯一,所以这里我们调用$_SERVER['REQUEST_URI'并用MD5进行加密处理既可做到唯一#

      删除缓存

            $tpl ->iscached("模板文件"[,缓存id])  判断缓存文件是否存在

            $tpl -> clear_cache("模板文件"[,缓存id])   删除某个缓存文件

            $tpl->clear_all_cache();  全部删除

    <?php
    if($tpl -> iscached("contents.html",$cache_id)){
        $tpl -> clearCache("contents.html",$cache_id);
        $tpl -> clearAllCache();
    }
    ?>
  • 相关阅读:
    《机器学习》周志华 习题答案8.5
    《机器学习》周志华 习题答案8.3
    《机器学习》周志华 习题答案7.3
    《机器学习》周志华 习题答案6.2
    《机器学习》周志华 习题答案4.3
    Python使用wxPython、py2exe编写桌面程序-乾颐堂
    python生成验证码,文字转换为图片-乾颐堂
    python使用wmi模块获取windows下的系统信息监控系统-乾颐堂
    Python图像处理库:Pillow 初级教程-乾颐堂
    python的metaclass浅析-乾颐堂
  • 原文地址:https://www.cnblogs.com/shuo-128/p/6876119.html
Copyright © 2011-2022 走看看