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技术了

  • 相关阅读:
    docker基础:docker网络模式
    WEB架构师成长之路之一-走正确的路(转载)
    DDD(领域驱动设计)
    C#泛型和泛型约束(转载)
    MES系统介绍
    vue中 computed和watch的一些简单理解(区别)(转载)
    sqlserver常用表值函数
    SQLServerAgent 当前未运行,因此无法将此操作通知它。
    浅谈敏捷开发(转载)
    认证、授权、鉴权和权限控制(转载)
  • 原文地址:https://www.cnblogs.com/doyourself/p/4065167.html
Copyright © 2011-2022 走看看