ActiveDataProvider caching

I have a REST-controller like in Guide (

How I may cache data (ActiveDataProvider) returned by prepareDataProvider method?

Some code:

public function actions()


	$actions = parent::actions();


	$actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];

	return $actions;


public function prepareDataProvider()


	$query = Users::find()


	return new ActiveDataProvider([

		'query' => $query,

		'pagination' => [

			'pageSize' => 50




I would also like to know how to do it.

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))




		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