How to update cache asynchronously ?

I am caching my homepage, and I would like to update cache every 60 seconds. By default yii will update cache when first user visits the page after 60 seconds. I want this to be only fallback behaviour in case my precache didn’t worked. I do not want user to experience lower response time, because he is waiting for cache to update ( executing all queries in behind ). I want to trigger cache update every 60 seconds by cron script. This means that while the new cache is being generated the old one is being served to the users. Is this somehow possible in yii ?

In first case, I am using page caching like this:




    public function behaviors()

    {

        return \yii\helpers\ArrayHelper::merge(parent::behaviors(), [

            [

                'class' => 'yii\filters\PageCache',

                'only' => ['index'],

                'duration' => 60,

                'variations' => [

                    \Yii::$app->language,

                ],

            ],

        ]);

    }



How can I pregenerate content for my index page every 60 sec ? Please remember that old cache need to be served while the new one is being created.

If this is possible, how can one do this with data and fragment cache ? Any tips or guide on how to do it ?

Sure.

  1. Implement a console command which updates cache and sets with with expiration time of 60 seconds. Make sure cache is shared between web app and console app. Add it to cron.

  2. Set web cache expiration time to 1 minute (so it won’t be triggered normally at all).

Thanks.

  1. What do you mean by "Make sure cache is shared between web app and console app." ? Is this a matter of configuration ?

Yes. Cache component should have the same configuration. Also it should not be APC.