Merge pull request #2944 from Roardom/announce-prepared-statement

(Update) Use prepared statement instead of eager loading peers
This commit is contained in:
HDVinnie
2023-07-23 11:50:06 -04:00
committed by GitHub
+10 -5
View File
@@ -388,11 +388,6 @@ class AnnounceController extends Controller
$torrentId = Redis::connection('cache')->command('HGET', [$cacheKey, hex2bin($infoHash)]);
$torrent = Torrent::withoutGlobalScope(ApprovedScope::class)
->with([
'peers' => fn ($query) => $query
->select(['id', 'torrent_id', 'peer_id', 'user_id', 'left', 'seeder', 'port', 'updated_at'])
->selectRaw('INET6_NTOA(ip) as ip')
])
->select(['id', 'free', 'doubleup', 'seeders', 'leechers', 'times_completed', 'status'])
->when(
$torrentId === null,
@@ -426,6 +421,16 @@ class AnnounceController extends Controller
new TrackerException(151, [':status' => 'POSTPONED In Moderation'])
);
// Don't use eager loading so that we can make use of mysql prepared statement caching.
// If we use eager loading, then laravel will use `where torrent_id in (123)` instead of `where torrent_id = ?`
$torrent->setRelation(
'peers',
Peer::select(['id', 'torrent_id', 'peer_id', 'user_id', 'left', 'seeder', 'port', 'updated_at'])
->selectRaw('INET6_NTOA(ip) as ip')
->where('torrent_id', '=', $torrent->id)
->get()
);
return $torrent;
}