info('No buffered counters to synchronize.'); return 0; } $dirtyIds = array_unique(array_filter($dirtyIds)); $processed = 0; foreach ($dirtyIds as $id) { $totalKey = "{$prefix}total:{$id}"; $uniqueKey = "{$prefix}unique:{$id}"; $totalDelta = (int) Cache::pull($totalKey, 0); $uniqueDelta = (int) Cache::pull($uniqueKey, 0); if ($totalDelta > 0 || $uniqueDelta > 0) { // Perform a single atomic update query for this URL ShortUrl::where('id', $id)->update([ 'total_visits' => DB::raw("total_visits + {$totalDelta}"), 'unique_visits' => DB::raw("unique_visits + {$uniqueDelta}"), ]); $processed++; } } $this->info("Successfully synchronized counters for {$processed} short URLs."); return 0; } }