Hello, I have one issue with caching.
If I run something like this:
$model = new Article();
$articles = $model->cache(100)->byCategory(Category::model()->findByPk(1))->findAll();
Actually will be cached Category model, not Article. Because cache() method only tells CDbConnection, to cache next request.
P.S. I know, this is not the cleanest example, but it is very simple to understand, what is wrong.
Caching should be applied only before actual find. I did some hack for this:
protected $_cacheOptions;
public function cache($duration, $dependency=null, $queryCount=1)
{
$this->_cacheOptions = array(
'duration' => $duration,
'dependency' => $dependency,
'queryCount' => $queryCount,
);
return $this;
}
public function applyScopes(&$criteria)
{
parent::applyScopes($criteria);
// Adds caching after all scopes
if($this->_cacheOptions) {
parent::cache($this->_cacheOptions['duration'], $this->_cacheOptions['dependency'],
$this->_cacheOptions['queryCount']);
}
}
Somebody will ask why I moved this to applyScopes, instead of beforeFind(). This is because beforeFind is executed before applyScopes, what is actually wrong too.
This will be really nice, if it can be fixed in 1.1.11 release. Because this is very dirty hack. Because, when I tell to cache article model, I want to cache it exactly, not next request.
I have spend long time yesterday in very similar situation understanding why did my model does not caches.