YII 框架 第九天(4)數(shù)據(jù)緩存
說是很重要的,經(jīng)常使用數(shù)據(jù)緩存,而不是其他的片段和頁面緩存,額好像我不太喜歡這種緩存額
????????????function?getGoodsInfo($id) ????????????{ ????????????????$info?=?Yii::app()->cache->get('goods_info'.$id);//為什么要加id呢,因?yàn)橐啥鄠€(gè)緩存頁面?goods/detail&id=1?goods/detail&id=2? ?????????????????????????????????????????????????????????????????//如果沒有加id,那么?goods/detail&id=3?會(huì)和id=1一樣,是同一個(gè)緩存頁面 ????????????????if(!empty($info)) ????????????????{ ????????????????????return?$info; ????????????????} ????????????????$info?=?Goods::model()->findByPk($id); ????????????????Yii::app()->cache->set('goods_info'.$id,$info,10); ????????????????return?$info; ????????????}
不帶緩存情況下的Detail
????????public?function?actionDetail($id) ????????{ ????????????$goods_model?=?Goods::model(); ????????????$goods_info?=?$goods_model->findByPk($id); ????????????$this->render('detail',array('goods_info'=>$goods_info)); ????????}
帶緩存的Detail
????????public?function?actionDetail($id) ????????{ ????????????$goods_model?=?Goods::model(); ????????????$goods_info?=?$goods_model->getGoodsInfo($id); ????????????$this->render('detail',array('goods_info'=>$goods_info)); ????????}