forge/app/Jobs/UpdateModDownloadsJob.php
Refringe 16e3a67efd
Query Optimization
- Download counts were taking too long to calculate dynamically, so we're keeping track of a total count with observers and queued job.
- Optimized the SQL used to order a mod listing by mod version update times.
2024-08-31 01:19:22 -04:00

28 lines
665 B
PHP

<?php
namespace App\Jobs;
use App\Models\Mod;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class UpdateModDownloadsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Recalculate the total download counts for each mod.
*/
public function handle(): void
{
Mod::with('versions')->chunk(100, function ($mods) {
foreach ($mods as $mod) {
$mod->calculateDownloads();
}
});
}
}