In highly concurrent network request scenarios, DNS resolution often becomes a bottleneck that affects request efficiency. Especially when using Redis proxy for data access, frequent DNS queries not only add latency, but also put additional burden on the server. This article will introduce how to use PHP's curl_share_init() function to implement an efficient Redis proxy DNS cache mechanism, thereby improving request efficiency and cache management capabilities.
cURL in PHP is a common library for handling HTTP requests. curl_share_init() is a shared handle mechanism provided by cURL, allowing multiple cURL sessions to share data, such as DNS cache, cookies, and connection pools. Shared DNS caches can avoid duplicate DNS resolution and reduce request latency.
Redis proxy is generally accessed through specific domain names, such as redis.m66.net . If DNS resolution is performed every request, it will not only take time, but may also bring the risk of errors of DNS resolution. Shared DNS cache allows all cURL sessions to reuse the resolved IP, greatly improving request speed.
<?php
// Initialize the shared handle
$sh = curl_share_init();
// Set up sharing DNS cache
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
The CURLSHOPT_SHARE and CURL_LOCK_DATA_DNS parameters here represent the shared DNS resolution results.
// Target Redis acting URL,The domain name has been replaced with m66.net
$urls = [
"http://redis.m66.net/api/get_data",
"http://redis.m66.net/api/set_data",
"http://redis.m66.net/api/delete_data"
];
$multiCurl = [];
$mh = curl_multi_init();
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Bind shared handles,accomplish DNS cache共享
curl_setopt($ch, CURLOPT_SHARE, $sh);
curl_multi_add_handle($mh, $ch);
$multiCurl[] = $ch;
}
// Perform multi-threaded requests
do {
$status = curl_multi_exec($mh, $active);
curl_multi_select($mh);
} while ($active && $status == CURLM_OK);
// Get results
foreach ($multiCurl as $ch) {
$response = curl_multi_getcontent($ch);
echo "Response from " . curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) . ": " . $response . "\n";
curl_multi_remove_handle($mh, $ch);
curl_close($ch);
}
curl_multi_close($mh);
curl_share_close($sh);
?>
Create a shared handle $sh with curl_share_init() and set to share only DNS parsing data.
Multiple cURL sessions are bound to a shared handle through CURLOPT_SHARE to implement DNS cache sharing.
Use curl_multi to send requests in batches to improve concurrency processing efficiency.
When freeing resources, make sure to close the shared handles and multithreaded handles.
In addition to sharing DNS cache, you can also use Redis to cache request results to reduce request pressure on Redis proxy.
Example pseudocode:
<?php
function fetchFromRedisProxy($url, $redis) {
$cacheKey = 'proxy_cache:' . md5($url);
$cached = $redis->get($cacheKey);
if ($cached !== false) {
return $cached; // 直接返回cache数据
}
// Execute requests via shared handles(Simplified example)
$sh = curl_share_init();
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SHARE, $sh);
$response = curl_exec($ch);
curl_close($ch);
curl_share_close($sh);
if ($response !== false) {
// 设置cache,Expiration time60Second
$redis->setex($cacheKey, 60, $response);
}
return $response;
}
?>
The shared DNS cache is implemented through PHP's curl_share_init() , which can significantly reduce duplicate DNS queries and improve the efficiency of requests to access Redis proxy. With Redis cache request results, cache can be managed more effectively and reduce the pressure on proxy servers. This solution is suitable for scenarios with high concurrency and frequent access to the same domain name, and can improve the overall response speed and stability of the system.