如何通过标签重置PageCache?如何标记PageCache?

时间:2019-05-20 11:00:33

标签: caching yii2

我在我的控制器中使用具有某些sql依赖性的PageCache类型。我需要一键重置缓存中的控制器。我该如何标记?

public function behaviors()
    {
        return [
            [
                'class' => 'yii\filters\PageCache',
                'only' => ['category','index','tags','tag', 'post'],
                'duration' => Yii::$app->params['blog_section'],
                'variations' => [
                    Yii::$app->request->get('code'),
                    \Yii::$app->request->get('page'),

                ],
                'dependency' => [
                    'class' => 'yii\caching\DbDependency',
                    'sql' => 'SELECT MAX(updated_at) FROM post',
                ],

            ],
        ];
    }

1 个答案:

答案 0 :(得分:1)

您可以将TagDependencyChainedDependencyDbDependency组合使用:

'dependency' => [
    'class' => 'yii\caching\ChainedDependency',
    'dependencies' => [
        new \yii\caching\DbDependency([
            'sql' => 'SELECT MAX(updated_at) FROM post',
        ]),
        new \yii\caching\TagDependency([
            'tags' => 'posts',
        ]),
    ],
],

SELECT MAX(updated_at) FROM post结果更改或名称为TagDependency的{​​{1}}更改时,缓存将重新生效。您可以通过以下方式重新验证标签:

posts