Skip to content
Advertisement

Yii2 : how to cache active data provider?

In my PostSearch model I have this code :

JavaScript

my try, instead of above line return $dataProvider, would be this block of code:

JavaScript

I would like to cache the result returned by ADP, based on the updated_at field. I mean I want to serve data from cache until some change is made. My code does not work, I mean caching is not applied at all. What I am doing wrong, and is it possible to do this on ADP ? Thanks

Advertisement

Answer

It has little use caching the data provider after instantiating, since it’s not actually doing any selecting on the database until it has been prepared. So you would actually be caching an empty object instance like it is now.

If you have a very large set of records, call the dataProviders’ prepare() in advance in the cache:

JavaScript

This will actually cache whatever queries the dataProvider runs ,so the next time they will be fetched from the query cache. This should result in what you are looking for.

If you have a finite amount of records, caching them all at once could also work:

JavaScript

Obviously this is less than ideal for larger datasets, but it depends on what you are looking for.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement