mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-04 17:21:39 +09:00
Cache and reuse the Guzzle client for multiple requests
This commit is contained in:
parent
866d704d0d
commit
d731198aa6
1 changed files with 18 additions and 2 deletions
|
|
@ -12,6 +12,19 @@ class HTTP
|
|||
*/
|
||||
public const DEFAULT_TIMEOUT = 3;
|
||||
|
||||
/**
|
||||
* Cache the Guzzle client instance here.
|
||||
*/
|
||||
protected static $_client = null;
|
||||
|
||||
/**
|
||||
* Reset the Guzzle client instance.
|
||||
*/
|
||||
public static function resetClient(): void
|
||||
{
|
||||
self::$_client = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a GET request.
|
||||
*
|
||||
|
|
@ -160,9 +173,12 @@ class HTTP
|
|||
}
|
||||
|
||||
// Send the request.
|
||||
$guzzle = new \GuzzleHttp\Client();
|
||||
if (!self::$_client)
|
||||
{
|
||||
self::$_client = new \GuzzleHttp\Client();
|
||||
}
|
||||
$start_time = microtime(true);
|
||||
$response = $guzzle->request($method, $url, $settings);
|
||||
$response = self::$_client->request($method, $url, $settings);
|
||||
$status_code = $response->getStatusCode() ?: 0;
|
||||
|
||||
// Measure elapsed time and add a debug entry.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue