smarty-cache

smarty-cache

cache?

It is to save the content of the page to the disk, and the next time the same page is accessed, the saved content is directly returned, which reduces the pressure on the database.


require('../smarty3/smaty.class.php');
require('../mySmaty.class.php');

$smarty = new MySmarty();


//思考:
// 如下数据,短期内不会变化,比如1小时,很少变化,而一小时内却有1000位用户访问
// 那么,这1000次,每次都是同样的内容,但是访问了数据库1000次
// 这时,就产生了性能上的浪费。这时就可以开启smarty缓存了


{
    $conn = mysql_connect('localhost','root','111111');
    mysql_query("set names utf8");
    mysql_query("set shop");


    $sql = "select goods_id,goods_name,goods_price from  goods limit 5";
    $rs = mysql_query($sql,$conn);

    $goods = array();
    while($row = mysql_fetch_assoc($rs)){
        $goods[] = $row;
    }

    // 最终,$goods 数组如下所示
    $goods = array('0'=>array('goods_id'=>'1','goods_name'=>'iPad air2','goods_price'=>'4673'),
    '2'=>array('goods_id'=>'2','goods_name'=>'iPhone5s','goods_price'=>'3300'));

   echo "我走了数据库";
}



$smarty->assign('goods',$goods);
$smarty->display('demo.html');

demo.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    {literal}
        缓存
    {/literal}

    <table>
        <tr><td>序号</td><td>商品名</td><td>价格(元)</td></tr>

        {foreach $goods as $key=>$item}
        <tr><td>{$item.goods_id}</td><td>{$item.goods_name}</td><td>{$item.goods_price}</td></tr>
        </foreach>

    </table>
</body>
</html>

usage of smarty page cache

  1. turn on
  2. Configure the cache life cycle
  3. Set the cache directory for storing cache files
  4. Determine whether to cache and whether to fetch data from the database.
  5. output

require('../smarty3/smaty.class.php');
require('../mySmaty.class.php');

$smarty = new MySmarty();

// 1. 开启缓存
$smarty->caching = true;

// 2. 设置一个缓存周期
$smarty->cache_lifetime = 3600;

// 3. 设置缓存目录,用于存储缓存文件
$smarty->cache_dir = './cache';


if(!$smarty->isCached('demo.html')){ // 4. 判断demo.html是否已经被缓存了
    $conn = mysql_connect('localhost','root','111111');
    mysql_query("set names utf8");
    mysql_query("set shop");


    $sql = "select goods_id,goods_name,goods_price from  goods limit 5";
    $rs = mysql_query($sql,$conn);

    $goods = array();
    while($row = mysql_fetch_assoc($rs)){
        $goods[] = $row;
    }

    // 最终,$goods 数组如下所示
    $goods = array('0'=>array('goods_id'=>'1','goods_name'=>'iPad air2','goods_price'=>'4673'),
    '2'=>array('goods_id'=>'2','goods_name'=>'iPhone5s','goods_price'=>'3300'));

   echo "我走了数据库";

   $smarty->assign('goods',$goods);
}


// 思考:
// 如果我在页面中需要显示当前时间,该如何,它会把时间也缓存
// 这就需要用到smarty的局部缓存
// smarty在页面缓存的情况下,可以设置部分内容不缓存
// 因为页面的某部分,如随机广告,股票信息,时间等

$smarty->display('demo.html');

The usage of smarty page local cache

  1. Control in the template (demo.html), a tag is not cached
    • {$标签 nocache}
  2. Control a slice of labels not to be cached
    • {nocache} 这里是不缓存的内容标签 {/nocache}
  3. In php, the control is not cached, the third parameter is nocache, true means no cache

    • php $smarty->assign('time2',$time2,true);
  4. Controlled separately in php and in the template
    • php
      php
      function insert_welcome($parm){
      return "你好" . $parm['name'] . rand(1000,9999);
      }
    • in template{insert name = 'welcome' name='王五'}

Note: tags that are not cached are guaranteed to always get their value from php.

Single template with multiple caches

Get product information based on the goods_id of the address


$smarty = new MySmarty();

// 1. 开启缓存
$smarty->caching = true;
$smarty->cache_lifetime = 3600;
$smarty->cache_dir = './cache';

$goods_id = $_GET['goods_id'] + 0;

if(!$smarty->isCache('goods.html',$goods_id)){
    $conn = mysql_connect('localhost','root','111111');
    mysql_query("set names utf8");
    mysql_query("set shop");

    $sql = "select goods_id,goods_name,goods_price from  goods where goods_id=" . $goods_id;
    $rs = mysql_query($sql,$conn);

    $goods = mysql_fetch_assoc($rs);

    // 最终,$goods 数组如下所示
    $goods = array('0'=>array('goods_id'=>'1'));
    $smarty->assign('goods',$goods);
    echo "走了数据库";
}

$smarty->display('goods.html',$goods_id);

goods.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    {literal}
        单页面多缓存: 
            典型场景:
                为商品模板设置缓存,但是goods.php?id=N
                当缓存后,所有的商品页面都一样了,因为被缓存了,这显示不合适
            思考:
                能否为同一个模板,生成不同的缓存文件呢?
                比如,根据id的不同,来生成各个商品的缓存页面。
            答:
                可以,用到“单模板多缓存”
            原理:
                是生成缓存的时候,可以再传一个“缓存id”,如果id不同,生成的缓存文件则不同
            在何处传:
                在php页面中,$smarty->assign('goods.html',$goods_id);
            总结:
                你的哪些参数要影响页面的内容,就需要把哪些参数当成“缓存id”
                比如:page=3&cat=4 , 第四栏目的第三页,
                page和cat都要影响结果,因此这2个参数都要传入
            经典案例:
                ecshop商城,根据“缓存id”计算出真正的“缓存id”
    {/literal}

    <table>
        <tr><td>序号</td><td>商品名</td><td>价格(元)</td></tr>
        <tr><td>{$goods.goods_id}</td><td>{$goods.goods_name}</td><td>{$goods.goods_price}</td></tr>
    </table>
</body>
</html>

smarty cache force delete

$goods_id = $_GET['goods_id'] + 0;
$smarty = new MySmarty();

// 2个参数
// 第一个参数:模板名
// 第二个参数:缓存id
// 如果只指定模板名,不指定缓存id,则该模板对应得所有缓存都被删除
$smarty->clearCache('goods.html');

// 删除指定的缓存文件
$smarty->clearCache('goods.html',$goods_id);

echo "删除".$goods_id."号商品缓存成功";

Force update cache

$smarty = new MySmarty();
// 在php文件中,有时为了调试方便,临时不缓存,但又不想修改主代码,则可以加一个选项
$smarty->force_cache = true;
  • Refer to teacher Yan Shiba's smarty video tutorial, whose official website address is Boolean Education

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325725129&siteId=291194637