我在我的前端/SiteController.php中有这段代码。
在我执行'test'操作之后,进入了site/page?slug=home页面,但在日志中看到数据库计数为50,而应该是68。为什么invalidate方法不起作用?
编辑: 在common/config/main.php中,缓存设置如下:
public function actionPage($slug = 'home') {
Url::remember();
$val = Yii::$app->cache->getOrSet($slug, function() use ($slug) {
$model = DBMenu::find()->with('pagesTranslations')->where(['slug' => $slug])->one();
if ($model === null) {
return $this->redirect(['/site/page', 'slug' => 'home']);
}
return $model;
}, 0, new TagDependency(['tags' => 'page']));
$searchModel = new Search();
return $this->render('page',[
'model' => $val,
'searchModel' => $searchModel
]);
}
这段代码位于backend/PageController.php文件中
public function actionTest() {
TagDependency::invalidate(Yii::$app->cache, 'page');
}
在我执行'test'操作之后,进入了site/page?slug=home页面,但在日志中看到数据库计数为50,而应该是68。为什么invalidate方法不起作用?
编辑: 在common/config/main.php中,缓存设置如下:
'cache' => [
'class' => 'yii\caching\FileCache',
],
$val = Yii::$app->cache->get(...); if ($val === false) { $val = ...; Yii::$app->cache->set(...); }
。为什么在闭包内部进行重定向? - BizleycachePath
配置缓存。默认情况下,它的值为'cachePath' => '@runtime/cache'
。您可以设置@frontend/runtime/cache
,将所有缓存存储在前端文件夹中。 - Bizley