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

  • 相关阅读:
    关于JSON可能出现的错误,待更/todo
    mongoose的安装与使用(书签记录) 2017
    HTTP的学习记录3--HTTPS和HTTP
    HTTP的学习记录(二)头部
    HTTP(一)概述
    LeetCode 455. Assign Cookies
    LeetCode 453. Minimum Moves to Equal Array Elements
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 447. Number of Boomerangs
    LeetCode 416. Partition Equal Subset Sum
  • 原文地址:https://www.cnblogs.com/doyourself/p/4065167.html
Copyright © 2011-2022 走看看