标签:blog io 使用 文件 数据 on 2014 log cti
【数据缓存】清空:Yii::app()->cache->flush();
简单示例:
function actionHuan1(){ //设置变量缓存 Yii::app()->cache->set('username','zhangsan',3600); Yii::app()->cache->set('useraddr','beijing',3600); Yii::app()->cache->set('hobby','lanqiu',3600); echo "set cache is ok"; } function actionHuan2(){ //使用变量缓存 echo Yii::app()->cache->get('username'),"<br />"; echo Yii::app()->cache->get('useraddr'),"<br />"; echo Yii::app()->cache->get('hobby'),"<br />"; echo "use cache is ok"; } function actionHuan3(){ //删除缓存变量 //Yii::app()->cache->delete('username'); //清空缓存变量,也可以删除片段缓存或文件缓存 Yii::app()->cache->flush(); }
具体示例:
在模型中关于sql查询
function getGoodsInfoByPk($id){ //把获得的具体详细商品信息存入缓存,下次再来获得信息就去缓存读取 $info = Yii::app()->cache->get('goods_info'.$id);//获得缓存信息 //判断缓存信息有无 if(!empty($info)) return $info; $sql = "select * from {{goods}} where goods_id='$id'"; $goods_info = $this->findBySql($sql); //设置缓存 Yii::app()->cache->set('goods_info'.$id,$goods_info,3600); return $goods_info; }
标签:blog io 使用 文件 数据 on 2014 log cti
原文地址:http://blog.csdn.net/buyingfei8888/article/details/40517073