Skip to content

Commit

Permalink
dev: save some db calls by caching results
Browse files Browse the repository at this point in the history
  • Loading branch information
mennodekker committed Nov 11, 2012
1 parent 3657fba commit 47d89ee
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions application/models/Settings_global.php
Expand Up @@ -16,6 +16,7 @@

class Settings_global extends CActiveRecord
{
protected $resultCache = array();
/**
* Returns the static model of Settings table
*
Expand Down Expand Up @@ -72,5 +73,25 @@ function updateSetting($settingname, $settingvalue)
}

}

public function findAll($condition = '', $params = array()) {
$result = parent::findAll($condition, $params);
if (empty($condition) && empty($params)) {
// Only is there a re no conditions, we cache the results
foreach($result as $row) {
$this->resultCache[$row['stg_name']] = $row;
}
}
return $result;
}

public function findByPk($pk, $condition = '', $params = array()) {
if (empty($condition) && empty($params)) {
if (!empty($this->resultCache) && array_key_exists($pk, $this->resultCache)) {
return $this->resultCache[$pk];
}
}
return parent::findByPk($pk, $condition, $params);
}
}
?>
16 changes: 16 additions & 0 deletions application/models/Survey.php
Expand Up @@ -353,4 +353,20 @@ public function deleteSurvey($iSurveyID, $recursive=true)
Quota::model()->deleteQuota(array('sid' => $iSurveyID), true);
}
}

public function findByPk($pk, $condition = '', $params = array()) {
static $lastResult = array();
if (empty($condition) && empty($params)) {
if (array_key_exists($pk, $lastResult)) {
return $lastResult[$pk];
} else {
$result = parent::findByPk($pk, $condition, $params);
$lastResult[$pk] = $result;

return $result;
}
}

return parent::findByPk($pk, $condition, $params);
}
}

0 comments on commit 47d89ee

Please sign in to comment.