DNS queries are usually time-consuming operations, especially when querying the same domain multiple times. To reduce frequent DNS queries, a caching mechanism can be used to store resolved DNS records. You can cache DNS query results in memory, for example using APCu or Memcached, or write the results to a database. Whenever a domain needs to be queried, first check if the relevant record exists in the cache. If it does, return the cached data directly without performing another DNS query.
$domain = 'example.com';
$cacheKey = 'dns_' . md5($domain);
<p>// Check cache<br>
if ($cachedRecord = apcu_fetch($cacheKey)) {<br>
$records = $cachedRecord;<br>
} else {<br>
// Perform DNS query<br>
$records = dns_get_record($domain);<br>
// Cache the query result<br>
apcu_store($cacheKey, $records, 3600); // Cache for 1 hour<br>
}</p>
<p>// Output DNS records<br>
print_r($records);<br>
The dns_get_record function supports various DNS query types (such as A, AAAA, MX, etc.). When performing DNS queries, it's best to limit the query type to avoid querying unnecessary information. This can reduce the complexity and time of the query. For instance, if you're only interested in an IP address, you can specify querying for A or AAAA records.
$domain = 'example.com';
$records = dns_get_record($domain, DNS_A); // Query only A record
<p>print_r($records);<br>
dns_get_record is a blocking operation, meaning it will wait for the DNS query to complete before returning results. If multiple domains need to be queried at the same time, consider using asynchronous queries to improve efficiency. By executing multiple DNS queries in parallel, overall query time can be reduced.
A common approach is to use the cURL extension to execute multiple DNS queries in parallel. For example, create a cURL session with multiple URL requests, and perform the next request as soon as the previous one completes. Although this does not directly use dns_get_record, it can improve performance through other means.
$domains = ['example.com', 'test.com', 'm66.net'];
$multiCurl = [];
$mh = curl_multi_init();
<p>// Create multiple cURL requests<br>
foreach ($domains as $index => $domain) {<br>
$multiCurl[$index] = curl_init();<br>
curl_setopt($multiCurl[$index], CURLOPT_URL, 'http://' . $domain);<br>
curl_setopt($multiCurl[$index], CURLOPT_RETURNTRANSFER, 1);<br>
curl_multi_add_handle($mh, $multiCurl[$index]);<br>
}</p>
<p>// Execute queries<br>
$running = null;<br>
do {<br>
curl_multi_exec($mh, $running);<br>
} while ($running > 0);</p>
<p>// Get results<br>
foreach ($domains as $index => $domain) {<br>
$response = curl_multi_getcontent($multiCurl[$index]);<br>
echo "Domain: $domain\n";<br>
echo $response . "\n";<br>
}</p>
<p>// Close connections<br>
foreach ($domains as $index => $domain) {<br>
curl_multi_remove_handle($mh, $multiCurl[$index]);<br>
}<br>
curl_multi_close($mh);<br>
By default, PHP's dns_get_record retrieves records via the system's DNS resolver. In high-frequency DNS query scenarios, using public DNS servers (like Google or Cloudflare) might introduce higher latency. Therefore, consider using a locally deployed DNS server to speed up DNS queries, especially in internal network environments, which can significantly reduce DNS query response times.
In high-concurrency environments, frequently querying certain domain names may cause slow DNS responses. To distribute the load, DNS load balancing can be employed by querying multiple DNS servers in rotation. You can manually select multiple DNS servers for queries or set up backup DNS servers in the application, which will automatically switch when the primary DNS server encounters issues.
$domains = ['example.com', 'm66.net'];
$dnsServers = ['8.8.8.8', '1.1.1.1']; // Backup DNS servers
$dnsServerIndex = 0;
<p>// Switch DNS server<br>
$records = dns_get_record($domains[0], DNS_A, $dnsServers[$dnsServerIndex]);<br>
print_r($records);<br>
In some cases, frequent DNS queries are unnecessary, especially when the query results don't change frequently. Optimizing performance can involve delaying queries or reducing the frequency of queries. For example, domain DNS records can be updated periodically instead of performing real-time queries each time. In situations where caching isn't applicable, you can set query intervals to avoid redundant DNS queries.
// Query DNS record once every 10 minutes
$lastChecked = time() - 600; // Assume the last query was 10 minutes ago
<p>if (time() - $lastChecked >= 600) {<br>
$records = dns_get_record('example.com');<br>
// Update query time<br>
$lastChecked = time();<br>
print_r($records);<br>
}<br>
The native PHP dns_get_record may not be suitable for all application scenarios. In certain situations, using a more efficient third-party library (such as php-dns, React\Dns, etc.) might offer better performance. These libraries typically provide asynchronous support, DNS caching, and more features, allowing you to choose the most appropriate library for your needs.
For example, React\Dns offers asynchronous DNS query functionality, allowing multiple DNS requests to be executed concurrently, thereby improving performance.