zoukankan      html  css  js  c++  java
  • 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 );
    }
    }

    }

  • 相关阅读:
    JavaScript脚本语言特色时钟
    这个周末安排,
    市场营销书籍推荐
    比较好的管理类书籍推荐
    如何培养自己的领导力?或许你该看看这些书
    十本最畅销的市场营销书籍,你看过几本?
    如何提高情商?答案可能在《情商必读12篇》这本书里
    如何管理好员工?
    做销售要看哪些书?《销售管理必读12篇》了解下
    管理书籍推荐,你看过哪些呢?
  • 原文地址:https://www.cnblogs.com/fengzhiqiangcaisangzi/p/3360731.html
Copyright © 2011-2022 走看看