Investigation and analysis of industry information page cache not updated in time
1. The industry information page cache has not been updated in time. When a new article is added in the background, the list page of the front desk is not updated in time, and it always takes 5 minutes to update. I feel that the dependency of the page cache has not taken effect. The latest two articles in the background are not updated in the foreground synchronously. as shown in Figure 1
2. But the same program is available normally in the local environment. After careful analysis of the differences, it is found that the update time of the articles in the background of the production environment is 8 hours earlier than that of Beijing time. If the updated_at is stored in UTC time (that is, 8 hours behind Beijing time), and you use Beijing time to make a judgment (for example, if the page is not updated), it will appear:
The data has actually been updated (15:30 Beijing time),
But updated_at is 2025-06-05 07:30:25 (UTC),
So the cache determines that the dependent max(updated_at) has not changed, and the page continues to use the old cache.
public function behaviors()
{
$request = Yii::$app->request;
$alias = $request->get('alias');
$id = $request->get('id');
$categoryId = (int)$request->get('c', 0);
$keyword = trim($request->get('k'));
$tag = $request->get('tag');
$page = (int)$request->get('page', 1);
$detect = new MobileDetect();
$isMobile = $detect->isMobile() ? PageElementType::DEVICE_TYPE_MOBILE : PageElementType::DEVICE_TYPE_PC;
$behaviors = [];
// view 页面缓存
if (!empty($alias)) {
$dependency = new DbDependency([
'sql' => 'SELECT updated_at FROM articles WHERE alias = :alias LIMIT 1',
'params' => [':alias' => $alias],
]);
} elseif (!empty($id)) {
$dependency = new DbDependency([
'sql' => 'SELECT updated_at FROM articles WHERE id = :id LIMIT 1',
'params' => [':id' => $id],
]);
} else {
$dependency = null;
}
$behaviors['pageCacheView'] = [
'class' => PageCache::class,
'only' => ['view'],
'duration' => 300,
'variations' => [$id, $alias, $isMobile],
'dependency' => $dependency,
'cache' => 'fileCache',
];
// index 页面缓存,仅当无关键词和标签时启用
if ($keyword === '' && empty($tag)) {
$category = null;
if (!empty($alias) && UrlHelper::isValidAlias($alias)) {
$category = Yii::$app->db->cache(fn() => ArticleCategory::find()
->where(['alias' => $alias, 'status' => CommonArticleCategory::STATUS_NORMAL])
->limit(1)
->one(), 300);
} elseif (!empty($categoryId)) {
$category = Yii::$app->db->cache(fn() => ArticleCategory::find()
->where(['id' => $categoryId, 'status' => CommonArticleCategory::STATUS_NORMAL])
->limit(1)
->one(), 300);
}
if ($category !== null) {
// 有分类筛选,仅依赖该分类的文章更新时间
$indexDependency = new DbDependency([
'sql' => 'SELECT MAX(updated_at) FROM articles WHERE status = 1 AND category_id = :categoryId',
'params' => [':categoryId' => $category->id],
]);
} else {
// 无分类筛选,才依赖全表(不建议再进一步切分,否则复杂度会上升)
$indexDependency = new DbDependency([
'sql' => 'SELECT MAX(updated_at) FROM articles WHERE status = 1',
]);
}
$behaviors['pageCacheIndex'] = [
'class' => PageCache::class,
'only' => ['index'],
'duration' => 300,
'variations' => [
$categoryId,
$alias,
$page,
$isMobile,
],
'dependency' => $indexDependency,
'cache' => 'fileCache',
];
}
return $behaviors;
}
3. Check the time zone settings in php.ini, which is equivalent to date.timezone = Asia/Shanghai. Decided to modify to date.timezone = asia/shanghai
date.timezone = PRC
4. Set the MySQL connection time zone to ensure that your PHP connects to the database to +08:00. You can do this in Yii2:
'components' => [
'db' => [
// ... 其他配置
'on afterOpen' => function($event) {
$event->sender->createCommand("SET time_zone = '+08:00'")->execute();
},
],
],
5. After editing an article, the update time is already the Beijing time zone. as shown in Figure 2

