zoukankan      html  css  js  c++  java
  • YII 1.0 缓存使用

    在main.php的组件components中配置设置添加缓存

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

    也就是framework/caching/CFileCache.php

    片段缓存

    在模板中使用

    <?php if($this->beginCache('bolg_index',array('duration'=>3600))): ?>
    .
    .
    .
     <?php $this->endCache();endif ?>
    

    整页缓存

    在控制器中设置 filters 方法,注意不要忘记了varyByParam,负责生成的页面都是一样的

    public function filters(){
            return array(
                array(
                    'system.web.widgets.COutputCache + index',
                    'duration' => 3600,
                    'varyByParam'=> array('id')
                )
            );
        }
    

    数据缓存

    数据缓存加分页实例

    public function actionIndex($id){
            $page = max(intval(@$_GET['page']),1);
            $data = Yii::app()->cache->get('blog_category_'.$id.'_'.$page);
            if($data == false){
                $model = Article::model();
                $total = $model->count("catid = $id");//统计总条数
    
                $pager = new CPagination($total);//实例化分页类
                $pager->pageSize = 4;//每页显示多少条
                $limit = (($page-1)*4).',4';
                //查询数据
                $info = $model->findAllBySql("select id,title,thumb,description,inputtime from {{article}} where catid = $id order by id desc limit $limit");
                $data = array("articleInfo"=>$info,"pages"=>$pager);
                Yii::app()->cache->set('blog_category_'.$id.'_'.$page, $data,3600);
            }
            $this->render("index",$data);
        }
    
  • 相关阅读:
    正则表达式
    什么是面向对象
    关于jdk,jre,jvm和eclipse的一些总结
    分析ajax爬取今日头条街拍美图
    pycharm误删恢复方法及python扩展包下载地址
    django 之 视图层、模板层
    django
    django框架基础二
    jdango框架基础一
    安装软件,提高速度,可以使用清华源
  • 原文地址:https://www.cnblogs.com/mr-amazing/p/4788488.html
Copyright © 2011-2022 走看看