When using PHP for network requests, we often use the cURL library. cURL is a powerful tool that not only supports simple HTTP requests, but also supports advanced features such as cookies, DNS, SSL, etc.
Among them, curl_share_init is a relatively rare cURL feature, which allows multiple cURL handles to share data, such as DNS cache, cookies, SSL session, etc. By sharing DNS cache, we can reduce duplicate domain name resolution and improve performance.
But in actual use, many people will ask:
Is the shared DNS cache effective? How to monitor it?
This article will take you to analyze step by step and give a complete PHP example.
The curl_share_init() function is used to initialize a shared handle. In conjunction with the CURLOPT_SHARE option of curl_setopt , it allows multiple cURL requests to share certain data. Common shared contents include:
DNS cache ( CURL_LOCK_DATA_DNS )
Cookie ( CURL_LOCK_DATA_COOKIE )
SSL session ( CURL_LOCK_DATA_SSL_SESSION )
For example:
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
In this way, multiple cURL handles bind the same $sh , and the DNS cache can be shared.
Although cURL does not provide an interface to "monitor DNS cache hits", we can observe indirectly:
First request time consuming : The first request usually contains DNS resolution time.
Time-consuming for subsequent requests : If shared DNS is enabled, subsequent requests for the same domain name should significantly reduce the resolution time.
In other words, we can use CURLINFO_NAMELOOKUP_TIME and CURLINFO_TOTAL_TIME to monitor:
Domain name resolution time
Total time consumption
By comparing the NAMELOOKUP_TIME of the first request and the subsequent request, you can determine whether the DNS cache is in effect.
Here is a complete PHP code using curl_share_init , sending two requests to https://m66.net/test , and printing the DNS resolution time:
<?php
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
$url = 'https://m66.net/test';
// First request
$ch1 = curl_init($url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_SHARE, $sh);
curl_exec($ch1);
$dns_time1 = curl_getinfo($ch1, CURLINFO_NAMELOOKUP_TIME);
$total_time1 = curl_getinfo($ch1, CURLINFO_TOTAL_TIME);
curl_close($ch1);
echo "First request:\n";
echo "DNS Analysis time: {$dns_time1} Second\n";
echo "Total time consumption: {$total_time1} Second\n";
// wait1Second,Simulate new requests
sleep(1);
// Second request
$ch2 = curl_init($url);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_SHARE, $sh);
curl_exec($ch2);
$dns_time2 = curl_getinfo($ch2, CURLINFO_NAMELOOKUP_TIME);
$total_time2 = curl_getinfo($ch2, CURLINFO_TOTAL_TIME);
curl_close($ch2);
echo "\nSecond request:\n";
echo "DNS Analysis time: {$dns_time2} Second\n";
echo "Total time consumption: {$total_time2} Second\n";
// Clean up
curl_share_close($sh);
?>
? Enable Sharing : Use curl_share_init and CURLSHOPT_SHARE to configure shared DNS data.
? Get time : Get CURLINFO_NAMELOOKUP_TIME through curl_getinfo .
? Comparative effect : The first request usually has obvious DNS time (such as dozens of milliseconds); the second time if the cache takes effect, NAMELOOKUP_TIME will be close to 0.
Same domain name : Only the same host name (such as m66.net ) can hit the cache.
PHP cURL version : Some older versions of PHP may not fully support curl_share . It is recommended to use phpinfo() to check the cURL version.
Thread safety : If you use curl_share in a multi-threaded or multi-process environment, you need to ensure locking security.