Skip to content

Commit

Permalink
Fixed issue ##19589: Multiple call of Survey->find a lot of time
Browse files Browse the repository at this point in the history
  • Loading branch information
Shnoulle committed Jun 26, 2024
1 parent ec62bea commit 1058634
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
12 changes: 12 additions & 0 deletions application/models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ public function relations()
);
}

/**
* @inheritdoc
* replace under condition $oQuestion->survey to use Survey::$findByPkCache
*/
public function getRelated($name, $refresh = false, $params = array())
{
if ($name == 'survey' && !$refresh && empty($params)) {
return Survey::model()->findByPk($this->sid);
}
return parent::getRelated($name, $refresh, $params);
}

/**
* @inheritdoc
* TODO: make it easy to read (if possible)
Expand Down
25 changes: 17 additions & 8 deletions application/models/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Survey extends LSActiveRecord implements PermissionInterface
*
* @var array $findByPkCache
*/
protected $findByPkCache = array();
protected static $findByPkCache;


// survey options
Expand Down Expand Up @@ -348,9 +348,7 @@ public function delete($recursive = true)
}

// Remove from cache
if (array_key_exists($this->sid, $this->findByPkCache)) {
unset($this->findByPkCache[$this->sid]);
}
$this->unsetFromStaticPkCache();

return true;
}
Expand Down Expand Up @@ -1002,12 +1000,12 @@ public function findByPk($pk, $condition = '', $params = array())
{
/** @var self $model */
if (empty($condition) && empty($params)) {
if (array_key_exists($pk, $this->findByPkCache)) {
return $this->findByPkCache[$pk];
if (isset(self::$findByPkCache[$pk])) {
return self::$findByPkCache[$pk];
} else {
$model = parent::findByPk($pk, $condition, $params);
if (!is_null($model)) {
$this->findByPkCache[$pk] = $model;
self::$findByPkCache[$pk] = $model;
}
return $model;
}
Expand All @@ -1016,12 +1014,23 @@ public function findByPk($pk, $condition = '', $params = array())
return $model;
}

/**
* Delete from static $findByPkCache
* return void
*/
public function unsetFromStaticPkCache()
{
if (isset(self::$findByPkCache[$this->sid])) {
unset(self::$findByPkCache[$this->sid]);
}
}

/**
* findByPk uses a cache to store a result. Use this method to force clearing that cache.
*/
public function resetCache()
{
$this->findByPkCache = array();
self::$findByPkCache = array();
}

/**
Expand Down

0 comments on commit 1058634

Please sign in to comment.