首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii-使用CSqlDataprovider进行缓存

Yii-使用CSqlDataprovider进行缓存
EN

Stack Overflow用户
提问于 2014-06-26 16:36:12
回答 1查看 232关注 0票数 0

在使用CSqlDataProvider时,是否可以缓存来自sql server查询的数据。如果是这样的话,任何人都可以提供一些相关文档的链接。或者,如果你亲自做过,请指导。

我搜索了一下,但什么也没找到:

EN

回答 1

Stack Overflow用户

发布于 2014-06-26 18:40:00

There is实现此功能的一些示例

代码语言:javascript
复制
<?php
class CachedSqlDataProvider extends CDataProvider
{
        public $queryCache;
        public $queryCacheLife;

        /**
         * @var CDbConnection the database connection to be used in the queries.
         * Defaults to null, meaning using Yii::app()->db.
         */

        public $db;
        /**
         * @var string the SQL statement to be used for fetching data rows.
         */
        public $sql;
        /**
         * @var array parameters (name=>value) to be bound to the SQL statement.
         */
        public $params=array();
        /**
         * @var string the name of key field. Defaults to 'id'.
         */
        public $keyField='id';

        /**
         * Constructor.
         * @param string $sql the SQL statement to be used for fetching data rows.
         * @param array $config configuration (name=>value) to be applied as the initial property values of this class.
         */
        public function __construct($sql,$config=array())
        {
                $this->sql=$sql;
                foreach($config as $key=>$value)
                        $this->$key=$value;
        }

        /**
         * Fetches the data from the persistent data storage.
         * @return array list of data items
         */
        protected function fetchData()
        {
                $sql=$this->sql;
                $db=$this->db===null ? Yii::app()->db : $this->db;
                $db->active=true;

                if(($sort=$this->getSort())!==false)
                {
                        $order=$sort->getOrderBy();
                        if(!empty($order))
                        {
                                if(preg_match('/\s+order\s+by\s+[\w\s,]+$/i',$sql))
                                        $sql.=', '.$order;
                                else
                                        $sql.=' ORDER BY '.$order;
                        }
                }

                if(($pagination=$this->getPagination())!==false)
                {
                        $pagination->setItemCount($this->getTotalItemCount());
                        $limit=$pagination->getLimit();
                        $offset=$pagination->getOffset();
                        $sql=$db->getCommandBuilder()->applyLimit($sql,$limit,$offset);
                }

                if( $this->queryCache == true && $this->queryCacheLife > 0 )
                        $command=$db->cache( $this->queryCacheLife )->createCommand($sql);
                else
                        $command=$db->createCommand($sql);

                foreach($this->params as $name=>$value)
                        $command->bindValue($name,$value);

                return $command->queryAll();
        }

        /**
         * Fetches the data item keys from the persistent data storage.
         * @return array list of data item keys.
         */
        protected function fetchKeys()
        {
                $keys=array();
                foreach($this->getData() as $i=>$data)
                        $keys[$i]=$data[$this->keyField];
                return $keys;
        }

        /**
         * Calculates the total number of data items.
         * This method is invoked when {@link getTotalItemCount()} is invoked
         * and {@link totalItemCount} is not set previously.
         * The default implementation simply returns 0.
         * You may override this method to return accurate total number of data items.
         * @return integer the total number of data items.
         */
        protected function calculateTotalItemCount()
        {
                return 0;
        }
}
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24425946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档