poor 2016-07-27 14:36:49 2892次浏览 3条回复 0 0 0

在视图里我写的片段缓存如图:

<?php

/* @var $this yii\web\View */

$this->title = 'My Yii Application';
?>
<div class="site-index">

    <div class="jumbotron">
        <h1>Congratulations!</h1>

        <p class="lead">You have successfully created your Yii-powered application.</p>

        <p><a class="btn btn-lg btn-success" href="http://www.yiiframework.com">Get started with Yii</a></p>
    </div>

    <div class="body-content">

        <div class="row">
            <div class="col-lg-4">
                <h2>Heading</h2>

                <p><?php
                if ($this->beginCache('key_indexs')) {
                        echo date('Y-d-d H:i:s');
                    
                 }       
                ?></p>

                <p><a class="btn btn-default" href="http://www.yiiframework.com/doc/">Yii Documentation &raquo;</a></p>
            </div>
            <div class="col-lg-4">
                <h2>Heading</h2>

                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
                    dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
                    ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
                    fugiat nulla pariatur.</p>

                <p><a class="btn btn-default" href="http://www.yiiframework.com/forum/">Yii Forum &raquo;</a></p>
            </div>
        </div>

    </div>
</div>

但浏览时生成的HTML代码如下:


<div class="site-index">

    <div class="jumbotron">
        <h1>Congratulations!</h1>

        <p class="lead">You have successfully created your Yii-powered application.</p>

        <p><a class="btn btn-lg btn-success" href="http://www.yiiframework.com">Get started with Yii</a></p>
    </div>

    <div class="body-content">

        <div class="row">
            <div class="col-lg-4">
                <h2>Heading</h2>

                <p><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-param" content="_csrf">
    <meta name="csrf-token" content="cGUtZE0xc2gTDEstAl07BkVIWSMaaDQ6ODQZLS9hQSYaE39VOhwyBw==">
    <title>My Yii Application</title>
    <link href="/assets/810e7c02/css/bootstrap.css" rel="stylesheet">
<link href="/css/site.css" rel="stylesheet">
<link href="/assets/66c469d4/toolbar.css" rel="stylesheet"></head>
<body>

<div class="wrap">
    <nav id="w0" class="navbar-inverse navbar-fixed-top navbar" role="navigation"><div class="container"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#w0-collapse"><span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span></button><a class="navbar-brand" href="/">My Company</a></div><div id="w0-collapse" class="collapse navbar-collapse"><ul id="w1" class="navbar-nav navbar-right nav"><li class="active"><a href="/site/index.html">Home</a></li>
<li><a href="/site/about.html">About</a></li>
<li><a href="/site/contact.html">Contact</a></li>
<li><form action="/site/logout.html" method="post">
<input type="hidden" name="_csrf" value="cGUtZE0xc2gTDEstAl07BkVIWSMaaDQ6ODQZLS9hQSYaE39VOhwyBw=="><button type="submit" class="btn btn-link">Logout (admin)</button></form></li></ul></div></div></nav>
    <div class="container">
                2016-27-27 14:20:41</p>

                <p><a class="btn btn-default" href="http://www.yiiframework.com/doc/">Yii Documentation &raquo;</a></p>
            </div>
            <div class="col-lg-4">
                <h2>Heading</h2>

                <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
                    dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
                    ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
                    fugiat nulla pariatur.</p>

                <p><a class="btn btn-default" href="http://www.yiiframework.com/forum/">Yii Forum &raquo;</a></p>
            </div>
        </div>

    </div>
</div>
    </div>
</div>

<footer class="footer">
    <div class="container">
        <p class="pull-left">&copy; My Company 2016</p>

        <p class="pull-right">Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a></p>
    </div>
</footer>

<div id="yii-debug-toolbar" data-url="/debug/default/toolbar.html?tag=579852b9d23e5" style="display:none" class="yii-debug-toolbar-bottom"></div><script src="/assets/a3ef32a9/jquery.js"></script>
<script src="/assets/3f2d3fc8/yii.js"></script>
<script src="/assets/810e7c02/js/bootstrap.js"></script>
<script src="/assets/66c469d4/toolbar.js"></script></body>
</html>

为什么beginCache前的HTML都跑到标签前面去了?

  • 回复于 2016-07-27 14:39 举报

    配置

    [php] view plain copy

    1. 'components' => [  
    2.     'cache' => [  
    3.             'class' => 'yii\caching\FileCache',  
    4.         ],  
    

    数据缓存

    [php] view plain copy

    1. $cache = \Yii::$app->cache;  
    2. $cache['aa'] = '123';  
    3. echo $cache['aa'];  
    

    片段缓存

    [php] view plain copy

    1. <h1>朝代</h1>  
    2. <?php if($this->beginCache($id, ['duration' => 3600])) { ?>  
    3. <?php  
    4. echo ListView::widget([  
    5.     'dataProvider' => $dataProvider,  
    6.     'itemView' => '_item',  
    7. ]);  
    8. ?>  
    9. <?php $this->endCache(); } ?>  
    10.   
    11. $dependency = [  
    12.     'class' => 'yii\caching\DbDependency',  
    13.     'sql' => 'SELECT MAX(updated_at) FROM post',  
    14. ];  
    15.   
    16. if ($this->beginCache($id, ['dependency' => $dependency])) {  
    17.   
    18.     // ... generate content here ...  
    19.   
    20.     $this->endCache();  
    21. }  
    22.   
    23. if ($this->beginCache($id, ['variations' => [Yii::$app->language]])) {  
    24.   
    25.     // ... generate content here ...  
    26.   
    27.     $this->endCache();  
    28. }  
    29.   
    30. if ($this->beginCache($id1)) {  
    31.   
    32.     // ...content generation logic...  
    33.   
    34.     if ($this->beginCache($id2, $options2)) {  
    35.   
    36.         // ...content generation logic...  
    37.   
    38.         $this->endCache();  
    39.     }  
    40.   
    41.     // ...content generation logic...  
    42.   
    43.     $this->endCache();  
    44. }  
    

    页面缓存

    [php] view plain copy

    1. class TestController extends Controller  
    2. {  
    3.     public function actionIndex()  
    4.     {  
    5.         //$db = \Yii::$app->db;  
    6.         sleep(2);  
    7.         $query = Dynasty::find();  
    8.         $dataProvider = new ActiveDataProvider([  
    9.                             'query' => $query,  
    10.                             'pagination' => [  
    11.                                 'pageSize' => 15,  
    12.                             ],  
    13.                         ]);  
    14.         return $this->render('index', [  
    15.             'dataProvider' => $dataProvider  
    16.         ]);  
    17.     }  
    18.     public function behaviors()  
    19.     {  
    20.         return [  
    21.             [  
    22.                 'class' => 'yii\filters\PageCache',  
    23.                 'duration' => 60,  
    24.                                 'variations'=> [$_GET['page']],  
    25.             ],  
    26.         ];  
    27.     }  
    28. }  
    

    动态内容

    [php] view plain copy

    1. ...别的HTML内容...  
    2. <?php if($this->beginCache($id)) { ?>  
    3. ...被缓存的片段内容...  
    4.     <?php $this->renderDynamic($callback); ?>  
    5. ...被缓存的片段内容...  
    6. <?php $this->endCache(); } ?>  
    7. ...别的HTML内容...  
    
  • 回复于 2016-07-27 14:40 举报

    已有配置:

     'cache' => [
                'class' => 'yii\caching\FileCache',
                'directoryLevel' => 2,
            ],
    
  • 回复于 2016-07-27 14:47 举报

    没加 $this->endCache();不好意思。。。

    觉得很赞
您需要登录后才可以回复。登录 | 立即注册