Yii的CFileCache详细分析

1.在配置文件main.php里面添加:

‘cache'=>array{

  'class'=>'CFileCache',

}

2.在控制器Controller的基类(假设为XFrontBase.php)里面,将要取出的数据设为属性,方法:

class XFrontBase extends Controller
{
protected $_conf;
protected $_seoTitle;
protected $_seoKeywords;
protected $_seoDescription;
protected $_catalog;
/**
* 初始化
* @see CController::init()
*/
public function init ()
{
parent::init();
//系统配置
$this->_catalog = Catalog::get(0, XXcache::system('_catalog'));
$this->_conf = XXcache::system('_config');
$this->_seoTitle = $this->_conf['seo_title'];
$this->_seoKeywords = $this->_conf['seo_keywords'];
$this->_seoDescription = $this->_conf['seo_description'];

}

}

3.建立XXcache缓存类:

<?php

class XXcache{

public static function system( $id, $expirse = 600, $fields = '', $params = array() ) {
$value = Yii::app()->cache->get( $id );
if ( $value === false ) {

//取出刷新出来的最新缓存
return self::_refresh( $id, $expirse, $fields, $params );
} else {
return $value;
}
}

//刷新缓存

public function _refresh( $id, $expirse, $fields, $params ) {
try {
switch ( $id ) {
case '_link':
$data = (array) self::_base( 'Link', $fields, array ( 'condition' => 'status_is=1' , 'order' => 'sort_order DESC,id DESC' ) );
self::set( $id, $data, $expirse );
break;
case '_pca':
$data = (array) self::_base( 'Pca', $fields, array ( 'condition' => "status_is='Y'" , 'order' => 'id ASC' ) );
self::set( $id, $data, $expirse );
break;
case '_userGroup':
$data = (array) self::_base( 'UserGroup', $fields );
self::set( $id, $data, $expirse );
break;
case '_ad':
$data = (array) self::_base( 'Ad', $fields, array ( 'condition' => 'status_is=1' , 'order' => 'sort_order DESC,id DESC' ) );
self::set( $id, $data, $expirse );
break;
case '_config':
$data = (array) self::_config( $params );
self::set( $id, $data, $expirse );
break;
case '_catalog':
$data = (array) self::_base( 'Catalog', $fields, array ( 'condition' => 'status_is=1' , 'order' => 'sort_order DESC,id DESC' ) );
self::set( $id, $data, $expirse );
break;
default:
throw new Exception( '数据不在接受范围' );
break;
}

return $data;
} catch ( Exception $error ) {
exit( $error->getMessage() );
}
}

protected function _base( $id = '', $fields = '', $condition = '' ) {
$mod = ucfirst( $id );
$model = new $mod();
$dataGet = $model->findAll( $condition );
foreach ( (array) $dataGet as $key => $row ) {
foreach ( (array) self::_attributes( $fields, $model ) as $attr ) {
$returnData[$key][$attr] = $row->$attr;
}
}
return $returnData;
}

protected function _config( $params = '' ) {
$configModel = Config::model()->findAll();
foreach ( (array) $configModel as $key => $row ) {
if ( $params['scope'] ) {
if ( in_array( $row['scope'], $params['scope'] ) ) {
$returnData[$row['variable']] = $row['value'];
}
}else {
$returnData[$row['variable']] = $row['value'];
}
}
return $returnData;
}

public static function set( $id = '', $data = '', $expirse = 3600 ) {
Yii::app()->cache->set( $id, $data, $expirse );
}

public static function get( $id ) {
$value = Yii::app()->cache->get( $id );
if ( $value === false ) {
return '';
} else {
return $value;
}
}

protected function _attributes( $fields, $model = '' ) {
if ( empty( $fields ) || trim( $fields ) == '*' ) {
return $model->attributeNames();
} else {
$fields = str_replace( ',', ',', $fields );
return explode( ',', $fields );
}
}

}

原文地址:https://www.cnblogs.com/fengzhiqiangcaisangzi/p/3360731.html