yii使用createCommand时查询不缓存

时间:2014-08-07 10:52:01

标签: caching yii memcached

我在mysql中记录了以下查询,该查询未被缓存。

SELECT
                        *,
                        round(priceperkm * IFNULL(KMPlanned, projects.KM),
                                4) AS NEWVALUE,
                        round(PRICE * IFNULL(KMPlanned, projects.KM),
                                2) AS TotalPriceForItem,
                        SUM(((abs(FSP - LSP) + 1) * SI) / 1000) AS KM_Completed,
                        round(SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE,
                                2) AS TotalPrice,
                        if(round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
                                2)>100,100,round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
                                2)) AS TotalPercent
                    FROM
                        hdb.projects
                            join
                        biditems ON projects.id = biditems.project_id
                            join
                        lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id
                            left join
                        jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id
                            left join
                        detailsseismic ON jobsprocesscomplete.JOBNO = detailsseismic.JOBNO
                                    where
                                    projects.PROJID = '1407075'
                                    and lookupprocess.ID = 16299

Yii代码如下。

    $dependancy = new CDbCacheDependency("select last_modified_date from projects where PROJID = $projid");
$countJobs = Jobs::model()->cache(CACHE_TIMEOUT,$dependancy)->count(array("condition"=>"PROJID=$projid"));
    foreach ($processstages as $k=>$v) {

$sql = "SELECT
                        *,
                        round(priceperkm * IFNULL(KMPlanned, projects.KM),
                                4) AS NEWVALUE,
                        round(PRICE * IFNULL(KMPlanned, projects.KM),
                                2) AS TotalPriceForItem,
                        SUM(((abs(FSP - LSP) + 1) * SI) / 1000) AS KM_Completed,
                        round(SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE,
                                2) AS TotalPrice,
                        if(round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
                                2)>100,100,round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, projects.KM)) * 100,
                                2)) AS TotalPercent
                    FROM
                        hdb.projects
                            join
                        biditems ON projects.id = biditems.project_id
                            join
                        lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id
                            left join
                        jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id
                            left join
                        details".$type['type']." ON jobsprocesscomplete.JOBNO = details".$type['type'].".JOBNO
                                    where
                                    projects.PROJID = :pid
                                    and lookupprocess.ID = :lid
                                    ";

    $command = Yii::app()->db->cache(CACHE_TIMEOUT,$dependancy)->createCommand($sql); // need to set memecahce to 50m to cache this query
                //Yii::log("select last_modified_date from projects where PROJID = $projid",CLogger::LEVEL_INFO, __METHOD__);
                $command->bindValue(":lid",$k,PDO::PARAM_INT);
                $command->bindValue(":pid",$projid,PDO::PARAM_INT);
                Yii::log((memory_get_peak_usage(true))/1024/1024 . "MB",CLogger::LEVEL_INFO, __METHOD__);
                $query = $command->query();
    }

我似乎无法弄清楚为什么它没有缓存这些结果,因为last_modified_date没有改变。我已将max item cache设置为32m,将memcache memory设置为512.

我已将我的查询重写为如下所示的缓存,但createCommand方式不会

$criteria=new CDbCriteria;
            $criteria->select = '
                                round(priceperkm * IFNULL(KMPlanned, t.KM),
                                            4) AS NEWVALUE,
                                    round(PRICE * IFNULL(KMPlanned, t.KM),
                                            2) AS TotalPriceForItem,
                                    SUM(((abs(FSP - LSP) + 1) * SI) / 1000) AS KM_Completed,
                                    round(SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE,
                                            2) AS TotalPrice,
                                    if(round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, t.KM)) * 100,
                                            2)>100,100,round((SUM(((abs(FSP - LSP) + 1) * SI) / 1000) * PRICE) / (PRICE * IFNULL(KMPlanned, t.KM)) * 100,
                                            2)) AS TotalPercent';
            $criteria->join = 'join biditems ON t.id = biditems.project_id ';
            $criteria->join .= 'join lookupprocess ON biditems.ITEMID = lookupprocess.biditems_id ';
            $criteria->join .= 'left join jobsprocesscomplete ON lookupprocess.ID = lookupprocess_id ';
            $criteria->join .= 'left join details'.$type['type'].' ON jobsprocesscomplete.JOBNO = details'.$type['type'].'.JOBNO ';
            $criteria->compare('t.PROJID',$projid);
            $criteria->compare('lookupprocess.ID',$k);

            Projects::model()->cache(CACHE_TIMEOUT,$dependancy)->findAll($criteria);

1 个答案:

答案 0 :(得分:0)

尝试进行以下操作:

$result = Yii::app()->db->cache(self::CACHE_TIMEOUT,$dependancy)
    ->createCommand($sql)
    ->bindValue(":lid",$k,PDO::PARAM_INT);
    ->bindValue(":pid",$projid,PDO::PARAM_INT)
    ->findAll();

我认为CACHE_TIMEOUT在这个类中是一个常量,但如果它在另一个类中定义,那么你需要调整这个部分