zoukankan      html  css  js  c++  java
  • smarty之缓存笔记

    smarty缓存技术


      在smarty中,缓存分为:普通缓存,单模版都缓存,局部缓存。

    缓存:
    1:首选打开缓存配置项:$smarty->caching=true;

    2:缓存生命周期的配置选项:$smarty->cache_lifetime=整秒数

    3:$smarty->is_cached('index.html')判断index.html页面是否缓存,如果缓存,避免io操作

     1 include('./libs/Smarty.class.php');
     2 $smarty=new Smarty();
     3 $conn=mysql_connect('127.0.0.1','root','');
     4 mysql_query('use smarty_cache',$conn);
     5 mysql_query('set nemes gbk',$conn);
     6 
     7 $smarty->caching=true;
     8 //开启缓存
     9 $id=$_GET['id']+0;
    10 $sql='select * from user where id='.$id;
    11 if(!$smarty->is_cached('index.html'))
    12 {
    13 $result=mysql_query($sql,$conn);
    14 $row=mysql_fetch_assoc($result);
    15     $smarty->assign('id',$row['id']);
    16     $smarty->assign('name',$row['name']);
    17     echo '走了数据库';
    18 }
    19 $smarty->display('index.html');


    单模版,多缓存

    1:缓存参数$smarty->display('index.html',$id);
    2:$smarty->is_cached('index.html',$id)判断index.html页面是否缓存,通过id来判断,id可以通过拼凑,可以参考ecshop中的缓存。
    原则:凡是能影响页面内容的变量,就要根据此变量cached_id

    include('./libs/Smarty.class.php');
    $smarty=new Smarty();
    $conn=mysql_connect('127.0.0.1','root','');
    mysql_query('use smarty_cache',$conn);
    mysql_query('set nemes gbk',$conn);
    
    $smarty->caching=true;
    //开启缓存
    $id=$_GET['id']+0;
    $sql='select * from user where id='.$id;
    $cached_id=$id+'XX'
    if(!$smarty->is_cached('index.html',$cached_id))
    {
    $result=mysql_query($sql,$conn);
    $row=mysql_fetch_assoc($result);
        $smarty->assign('id',$row['id']);
        $smarty->assign('name',$row['name']);
        echo '走了数据库';
    }
    $smarty->display('index.html',$cached_id);



    3:局部缓存:
    在模版中不需要缓存的地方,用{insert name='nocachedid'}来表示,那么该处最终的值就是,insert_nocachedid()函数的返回值


     <body>
        这是这是一个smarty缓存技术的页面页面<br/>
        {insert name=getname}
        编号:{$id}&nbsp;姓名:{$name}
    
     </body>
    include('./libs/Smarty.class.php');
    $smarty=new Smarty();
    $conn=mysql_connect('127.0.0.1','root','');
    mysql_query('use smarty_cache',$conn);
    mysql_query('set nemes gbk',$conn);
    function insert_getname()
    {
    
        return $_SESSION['name']
    }
    $smarty->caching=true;
    //开启缓存
    $id=$_GET['id']+0;
    $sql='select * from user where id='.$id;
    if(!$smarty->is_cached('index.html',$id))
    {
    $result=mysql_query($sql,$conn);
    $row=mysql_fetch_assoc($result);
        $smarty->assign('id',$row['id']);
        $smarty->assign('name',$row['name']);
        echo '走了数据库';
    }
    $smarty->display('index.html',$id);

    在ecshop中,用到了smarty模版技术,只能复习下smarty技术了

  • 相关阅读:
    Memento模式
    CSS实现半透明div层的方法
    JS解析json数据(如何将json字符串转化为数组)
    并发容器Map之二:ConcurrentNavigableMap
    JSinArray检查数组中是否存在某个值
    Servlet3.0之七:@WebFilter申明该类为过滤器
    Spring源码阅览——BeanFactory体系结构
    使用 Selenium RC 测试 web 应用程序
    函数式编程
    9 个 Java 处理 Exception 的最佳实践
  • 原文地址:https://www.cnblogs.com/doyourself/p/4065167.html
Copyright © 2011-2022 走看看