Created
October 4, 2019 08:58
-
-
Save shmurakami/561ddb2a5601dbca05f9409de3335d57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// snippet | |
// N+1 | |
private function bulkFetch($ids) | |
{ | |
$in = array_fill(0, count($ids), '?'); | |
$sth = $this->dbh->prepare('SELECT * FROM `users` WHERE `id` in (' . implode(',', $in) . ')'); | |
$r = $sth->execute($ids); | |
if ($r === false) { | |
throw new \PDOException($sth->errorInfo()); | |
} | |
$users = $sth->fetchAll(PDO::FETCH_ASSOC); | |
$result = []; | |
foreach ($users as $user) { | |
$id = $user['id']; | |
$result[$id] = [ | |
'id' => $id, | |
// ... | |
]; | |
} | |
return $result; | |
} | |
// guzzle async request | |
for ($i = 0; $i < 3; $i++) { | |
// http_errors -> do not throw exception | |
$promise = $client->getAsync('http://localhost:8080/index.php?id=' . $i, ['http_errors' => false]); | |
$promise->then( | |
function (\Psr\Http\Message\ResponseInterface $res) use (&$results) { | |
$results[] = [ | |
'status' => json_decode($res->getBody()->getContents())->status, | |
]; | |
} | |
//, function () {....} | |
); | |
$promises[] = $promise; | |
} | |
try { | |
$responses = all($plist)->wait(); | |
} catch (ClientException $e) { | |
$errors[] = json_decode($e->getResponse()->getBody()->getContents()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment