• smarty之缓存机制


    当smarty开启缓存时,当tpl文件内容改变时,则缓存页面会重新生成

    test.php:
    <?php
        date_default_timezone_set('asia/shanghai');
        header("Content-type: text/html; charset=utf-8");
        require 'smarty/Smarty.class.php';
        $_smarty = new Smarty();
        $_smarty->caching = true;
    
        $_smarty->assign('name','测试缓存');  
    
        $_smarty->display('test.tpl');
    ?>
    1 test.tpl:
    2 <html>
    3 <body>
    4 
    5 {$name} {$smarty.now|date_format:"%Y-%m-%d %H %M %S"}
    6 当此tpl文件内容改变时,则会 重新生成新缓存
    7 </body>
    8 </html>

    当smarty开启缓存时,当tpl文件内容不改变,而test.php文件改变时,则不生成缓存,虽然此种情况下不生成缓存,但是test.php文件里的内容也被执行了

     1 test.php:
     2 <?php
     3     date_default_timezone_set('asia/shanghai');
     4     header("Content-type: text/html; charset=utf-8");
     5     require 'smarty/Smarty.class.php';
     6     $_smarty = new Smarty();
     7     $_smarty->caching = true;
     8 
     9     $_smarty->assign('name','修改test.php的文件内容则不会生成缓存,虽然不生成缓存,但是test.php文件里的内容还是执行了,只是此行的assign函数没有重新解析!');  
    10         echo 123;//此行执行了
    11     $_smarty->display('test.tpl');
    12 ?>
    1 test.tpl:
    2 <html>
    3 <body>
    4 
    5 {$name} {$smarty.now|date_format:"%Y-%m-%d %H %M %S"}
    6 
    7 </body>
    8 </html>

    当smarty开启缓存时,tpl文件内容不变,而test.php中的$_smarty->display('test.tpl',$_SERVER["REQUEST_URI"]);里的display方法里的第二个参数值改变时,则test.php里的display方法会重新执行,则会重新生成缓存文件,并且生成的是新的缓存文件名如:一个页面有多篇文章的情况下:

    http://website/index.php?p=1
    http://website/index.php?p=2

     1 <?php
     2     date_default_timezone_set('asia/shanghai');
     3     header("Content-type: text/html; charset=utf-8");
     4     require 'smarty/Smarty.class.php';
     5     $_smarty = new Smarty();
     6     $_smarty->caching = true;
     7 
     8     $_smarty->assign('name','当参数值改变时则会重新生成一个新的缓存文件');  
     9 
    10     $_smarty->display('test.tpl',$_SERVER["REQUEST_URI"]);
    11 ?>

    使用is_cached()函数,这种是最常用的.即访问test.php时,当url地址不变,则直接执行缓存文件,而不再执行test.php里面的业务逻辑

     1 <?php
     2     date_default_timezone_set('asia/shanghai');
     3     header("Content-type: text/html; charset=utf-8");
     4     require 'smarty/Smarty.class.php';
     5     $_smarty = new Smarty();
     6     $_smarty->caching = true;
     7     //is_cached是否存在缓存
     8     //如果存在缓存,就不执行if语句里面的代码,否则就执行
     9     if(!$_smarty->is_cached('test.tpl',$_SERVER["REQUEST_URI"])) {
    10         echo '如果test.php的url不变,即使此if语句内的业务逻辑代码改变,也不会被执行,而是直接执行缓存文件';
    11     }
    12     
    13     $_smarty->assign('name','测试缓存');  
    14     $_smarty->display('test.tpl',$_SERVER["REQUEST_URI"]);
    15 ?>

    设置局部不缓存,比如,时间
    可以创建一个局部块,然后注册到模板中去,指定块包含的内容不缓存,由于块是被缓存过的,所以要设置块本身不被缓存才行, 可以在第三个参数指定为false才可以即:(测试时要清理编译文件和缓存文件)

     1 test.php:
     2 <?php
     3     
     4     require 'smarty.inc.php';
     5     function no_cache($_arr,$_content) {
     6         return $_content;
     7     }
     8     $_smarty->register_block('nocache','no_cache',true);
     9 
    10     $_smarty->display('test.tpl',$_SERVER["REQUEST_URI"]);
    11 ?>
    1 test.tpl:
    2 <html>
    3 <body>
    4     {nocache}
    5         {$smarty.now|date_format:"%Y-%m-%d %H %M %S"}
    6     {/nocache}
    7 </body>
    8 </html>

    为了方便起见,可以把局部缓存块做成插件形式,但要注意,块是默认被缓存的,需要在Smarty_Compiler.class.php里修改
    1,在plugins文件夹下注册块文件(函数):block.nocache.php

    1 function smarty_block_nocache($params, $content)
    2 {
    3     return $content;
    4 }

    2,查找$this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true);并修改成(714行代码处):

    1     if ($tag_command == 'nocache') {
    2         $this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, false);
    3     } else {
    4         $this->_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true);
    5     }
  • 相关阅读:
    0X01 OWASP WebGoat Splitting
    subprocess
    Python中getopt()函数的使用
    Python3_UDP客户端
    Python3编写TCP客户端
    Python3---pymysql库____操作数据库
    review——database (1)CH8-Relational Database Design
    删除的review——数据库 (1)CH6关系数据理论
    review——C# (15)转换
    review——C# (14)接口
  • 原文地址:https://www.cnblogs.com/xccjmpc/p/4184226.html
Copyright © 2020-2023  润新知