Answer the question
In order to leave comments, you need to log in
Yii2: How to make caching dependent on variable value?
Let me explain the question. I want to make news caching depending on their number. about here the problem is that if you do through
$dependency = new \yii\caching\DbDependency(['sql' => "SELECT COUNT(*) FROM news"]);
$query = News::find();
$countQuery = $query->count();
Answer the question
In order to leave comments, you need to log in
$query = News::find();
$countQuery = $query->count();
$data = Yii::$app->cache->getOrSet('newsCacheKey', function () use ($query) {
return $query->all();
}, $this->cacheDuration, new TagDependency([
'tags' => [
News::className() . $countQuery
]
]));
/yiisoft/yii2/caching/TagDependency.php
See if this works
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question