根据用户要求,用户日志系统的进度追踪机制已从使用缓存存储改为通过读取 user_logs 表的数据来确定处理进度。这种方式更加可靠和准确。
原来的方式(使用缓存):
// 从缓存获取进度
$lastProcessedId = Cache::get("user_log_collector:last_processed_id:{$sourceTable}", 0);
$lastProcessedTimestamp = Cache::get("user_log_collector:last_processed_timestamp:{$sourceTable}", 0);
// 更新进度到缓存
Cache::put("user_log_collector:last_processed_id:{$sourceTable}", $id, 86400);
Cache::put("user_log_collector:last_processed_timestamp:{$sourceTable}", $timestamp, 86400);
现在的方式(读取user_logs表):
// 从user_logs表获取最后处理的记录
$lastLog = UserLog::where('source_table', $sourceTable)
->where('source_type', $sourceType)
->orderBy('source_id', 'desc')
->first();
$lastProcessedId = $lastLog ? $lastLog->source_id : 0;
getLastProcessedId(): 改为从user_logs表查询最后处理的source_idgetLastProcessedTimestamp(): 改为从user_logs表查询最后处理的时间戳updateLastProcessedId(): 不再需要手动更新,进度自动追踪updateLastProcessedTimestamp(): 不再需要手动更新,进度自动追踪getOriginalRecordTimestamp() 抽象方法getOriginalRecordTimestamp() 方法getOriginalRecordTimestamp() 方法getOriginalRecordTimestamp() 方法,处理多表情况protected function getLastProcessedId(): int
{
try {
$lastLog = \App\Module\Game\Models\UserLog::where('source_table', $this->sourceTable)
->where('source_type', $this->sourceType)
->orderBy('source_id', 'desc')
->first();
return $lastLog ? $lastLog->source_id : 0;
} catch (\Exception $e) {
Log::error("获取最后处理ID失败", [
'collector' => $this->collectorName,
'source_table' => $this->sourceTable,
'error' => $e->getMessage()
]);
return 0;
}
}
由于农场收集器需要处理多个表(farm_harvest_logs 和 farm_upgrade_logs),进度查询逻辑特殊处理:
protected function getLastProcessedId(): int
{
try {
// 查询农场相关的最后处理记录
$lastLog = \App\Module\Game\Models\UserLog::where('source_type', $this->sourceType)
->whereIn('source_table', ['farm_harvest_logs', 'farm_upgrade_logs'])
->orderBy('source_id', 'desc')
->first();
return $lastLog ? $lastLog->source_id : 0;
} catch (\Exception $e) {
return 0;
}
}
protected function getOriginalRecordTimestamp(int $recordId): int
{
try {
$record = FundLogModel::find($recordId);
return $record ? $record->create_time : 0;
} catch (\Exception $e) {
return 0;
}
}
在 routes/console.php 中添加了用户日志收集的计划任务:
// 每分钟收集用户日志(实现高频收集)
\Illuminate\Support\Facades\Schedule::command('game:collect-user-logs --limit=100')->everyMinute();
虽然无法实现严格的"每2秒"执行,但每分钟执行可以保证较高的实时性。
# 显示收集器信息
php artisan game:collect-user-logs --info
# 显示统计信息
php artisan game:collect-user-logs --stats
# 执行收集(限制处理记录数)
php artisan game:collect-user-logs --limit=100
# 显示详细处理过程
php artisan game:collect-user-logs --detail
# 重置进度(清空user_logs表)
php artisan game:collect-user-logs --reset
--reset 选项会清空整个user_logs表,请谨慎使用通过这次优化,用户日志系统的进度追踪更加可靠和准确。系统不再依赖缓存存储进度信息,而是直接从实际的处理结果(user_logs表)中获取进度,确保了数据的一致性和可靠性。