Answer the question
In order to leave comments, you need to log in
How to set up caching in Yii2 in a relation method? And is it possible?
Code in the class:
public function getRoutes()
{
return $this->hasMany(Route::className(), ['place_id' => 'id']);
}
$db = self::getDb();
return $db->cache(function ($db){
return $this->hasMany(Route::className(), ['place_id' => 'id']);
});
return $this->hasMany(Route::className(), ['place_id' => 'id'])->cache(60)->all();
Answer the question
In order to leave comments, you need to log in
It is impossible to do this in the current version of the framework. It is better to move caching above the data retrieval layer and the higher the better. Good option www.yiiframework.com/doc-2.0/guide-caching-fragmen...
I'm still just learning too, but this is how it works for me exactly as it should:
The number of requests on list pages has been halved (username is displayed on each line).
True, I'm not sure that this is correct in terms of performance (a debatable question is who will be faster: a file cache or a database that also caches the same requests)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question