In highly concurrent PHP applications, frequent network requests for the same domain name may lead to a large number of DNS queries. This will not only increase the burden on the server side, but may also have a certain impact on performance. Fortunately, libcurl provides an efficient solution - the curl_share_init() function. By using it reasonably, we can implement DNS cache sharing, thereby reducing duplicate DNS queries and improving request efficiency.
curl_share_init() is a function provided by libcurl to initialize a shared handle (cURL share handle). Through this shared handle, we can share data between multiple cURL sessions, such as cookies, DNS caches, SSL sessions, etc. In particular, DNS cache sharing is of great significance to improving the performance of frequently requested the same domain name.
By default, a DNS query is performed to resolve the domain name every time a request is initiated using cURL. For example:
$ch = curl_init('https://m66.net/api/data');
curl_exec($ch);
curl_close($ch);
If the above request is repeatedly executed in a loop, DNS query will be triggered each time, causing unnecessary performance overhead, especially when requested frequently in a short period of time, this repeated resolution will significantly increase network latency and server pressure.
By sharing DNS cache, we can reuse the resolved domain names between multiple cURL handles, thus avoiding duplicate queries.
Here is an example showing how to use curl_share_init() to share a DNS cache:
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
for ($i = 0; $i < 10; $i++) {
$ch = curl_init('https://m66.net/api/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SHARE, $sh);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch) . PHP_EOL;
}
curl_close($ch);
}
curl_share_close($sh);
curl_share_init() : Initialize a shared handle.
curl_share_setopt() : Set the shared content to DNS cache.
curl_setopt($ch, CURLOPT_SHARE, $sh) : Bind the shared handle to each cURL request.
curl_share_close($sh) : Release the resource after use.
In this way, all cURL requests share the same DNS cache in a loop, avoiding the domain name resolution for each request, thereby greatly improving efficiency.
Shared handles cannot be used across threads (such as in multithreaded environments).
If the request involves multiple domain names, only requests for the same domain name can enjoy the benefits of DNS caching.
Shared resources must be closed correctly to avoid memory leaks.
Optimizing DNS queries can lead to significant performance improvements when handling high-frequency network requests. PHP uses cURL extension to call curl_share_init() and share the DNS cache, which is a simple and efficient optimization method. This is a trick worth trying for applications that are oriented towards high-performance requirements. By placing and using it properly, you can significantly reduce DNS pressure on the server and improve overall network response speed.