提交代码

This commit is contained in:
2025-06-06 18:42:27 +08:00
parent 7b4e41efc4
commit 25b765517a
2 changed files with 197 additions and 131 deletions
+15 -9
View File
@@ -57,12 +57,16 @@ class VisitStatsService
// 执行所有命令
$result = $pipe->exec();
// 更新数据库统计
$this->updateDailyStats($date, [
'total_visits' => $result[0],
'daily_visits' => $result[1],
'unique_visitors' => $this->getUniqueVisitors($date)
]);
// 使用Redis锁控制更新频率,每5分钟更新一次数据库
$lockKey = $this->prefix.'update_lock:'.$date;
if (!$this->redis->exists($lockKey)) {
$this->redis->setex($lockKey, 300, 1); // 5分钟锁
$this->updateDailyStats($date, [
'total_visits' => $result[0],
'daily_visits' => $result[1],
'unique_visitors' => $this->getUniqueVisitors($date)
]);
}
return [
'total' => $result[0],
@@ -91,7 +95,7 @@ class VisitStatsService
try {
// 获取其他统计数据
$otherStats = [
'total_users' => Db::name('users')->count(), // 移除 delete_time 条件
'total_users' => Db::name('users')->count(),
'new_users' => Db::name('users')->whereDay('create_time', $date)->count(),
'total_articles' => Db::name('articles')->where('delete_time', null)->count(),
'daily_articles' => Db::name('articles')->whereDay('create_time', $date)->count(),
@@ -101,8 +105,10 @@ class VisitStatsService
'resource_downloads' => Db::name('resources')->whereDay('update_time', $date)->sum('downloads')
];
// 记录日志,方便调试
Log::info('统计数据:' . json_encode($otherStats, JSON_UNESCAPED_UNICODE));
// 只在调试模式下记录日志
if (config('app.debug')) {
Log::info('统计数据:' . json_encode($otherStats, JSON_UNESCAPED_UNICODE));
}
// 合并统计数据
$stats = array_merge($stats, $otherStats);