Page Caching in Yii 2
Page Caching in Yii 2

Page Caching

Page caching refers to caching the content of a whole page on the server-side. Later when the same page is requested again, its content will be served from the cache instead of regenerating it from scratch.

Page caching is supported by [[yii\filters\PageCache]], an action filter. It can be used like the following in a controller class:



public function behaviors()
{
    return [
        [
            'class' => 'yii\filters\PageCache',
            'only' => ['index'],
            'duration' => 60,
            'variations' => [
                \Yii::$app->language,
            ],
            'dependency' => [
                'class' => 'yii\caching\DbDependency',
                'sql' => 'SELECT COUNT(*) FROM post',
            ],
        ],
    ];
}

The above code states that page caching should be used only for the index action. The page content should be cached for at most 60 seconds and should be variated by the current application language and the cached page should be invalidated if the total number of posts is changed.

As you can see, page caching is very similar to fragment caching. They both support options such as duration, dependencies, variations, and enabled. Their main difference is that page caching is implemented as an action filter while fragment caching a widget.

You can use fragment caching as well as dynamic content together with page caching.

Пет-проект как питомец: корми его минутами — он растёт вместе с тобой

Пишешь скрипты, настраиваешь серверы, ведёшь блог — но прогресс невидим. Пет-проект в WEB-[ло]мастерской требует регулярного внимания, как питомец: фиксируешь время и навыки — видишь, как растёт твоё мастерство в цифрах.

Подходит для обучения, экспериментов, side-проектов. Ставка часа обязательна даже для пет-проектов — это зеркало твоей самооценки. Данные на серверах в РФ, без спама и рассылок. Регистрация — меньше 2 минут.

Старт! MoonВase — A Hot Start on the Internet
Старт! MoonВase — A Hot Start on the Internet
Старт! Menu