SerjRamone
(Sergey Greznov)
1
I have a REST-controller like in Guide (http://www.yiiframework.com/doc-2.0/guide-rest-controllers.html).
How I may cache data (ActiveDataProvider) returned by prepareDataProvider method?
Some code:
public function actions()
{
$actions = parent::actions();
unset($actions['delete']);
$actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];
return $actions;
}
public function prepareDataProvider()
{
$query = Users::find()
->active();
return new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 50
],
]);
}
tehmaestro
(Alexnegrila89)
2
I would also like to know how to do it.
flarpy
(Peter)
3
This is what I am using
in config[‘components’] (changing to redis pre deployment)
'cache' => [
'class' => 'yii\caching\FileCache',
],
in search method
$query = $this->buildSearch($params);// returns instance of yii\db\Query;
$db = Yii::$app->db;
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
$models = $db->cache(function($db) use (&$query, &$params, &$pages){
return $query->offset(\yii\helpers\ArrayHelper::getValue($params, 'page', $pages->offset))
->limit($pages->limit)
->all();
});
return new ActiveDataProvider([
'query' => $query,
'pagination' => $pages,
'models' => $models,
]);
As you can see I’m not caching the pagination query yet, but could use the same approach