繁体   English   中英

Yii使用beginCache / endCache缓存主布局文件

[英]Yii Caching main layout file with beginCache/endCache

在Yii中,我的主要布局和$ content都有变化,因此我不需要缓存它。 我的想法是缓存main layout文件(包装$ content的部分,来自view文件):

 <? 
 $id= 'something';
 if($this->beginCache($id, array('duration'=>3600, 'variations' =>array(Yii::$app->language)))) {  ?>

 <!DOCTYPE html>
 <html>
 <head> 
 ... 
 <?php $this->endCache(); } // end of the upper cache piece ?>

     <?php echo $content; ?>

 <?php if($this->beginCache($id, array('duration'=>3600))) { // new cashe piece ?>              
        ... 
 </body>
 </html>
 <?php $this->endCache(); } ?>

设定:

config / main.php

'cache'=>array(
        'class'=>'system.caching.CFileCache',
            //  'connectionID'=>'db',
            //  'autoCreateCacheTable'=>false,
            //  'cacheTableName'=>'cache',
    ),
    'memcache'=>array(
       'class'=>'system.caching.CMemCache',
     ),

我已将缓存filters()放入主控制器中:

组件/ Controller.php

class Controller extends CController
{
   public function filters()
   {
    return array(
        'accessControl', // perform access control for CRUD operations
        'postOnly + delete', // we only allow deletion via POST request
        array(
            'COutputCache',
            'duration'=>1000,
            'varyByParam'=>array('id'),
        ),
     );
  } 

我不确定我是否做对了。 我还担心$id应该是哪种类型? 经过一番测试后,我发现性能没有大的提高...您能评论/纠正我吗?

您的配置文件似乎有误。 您已启用2个缓存? CFileCache和CMemCache?

查看参考资料: http : //www.yiiframework.com/doc/guide/1.1/en/caching.overview

正确的配置应为:

'cache'=>array( 'class'=>'system.caching.CFileCache', ),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM