【发布时间】:2020-11-04 17:08:37
【问题描述】:
example.com/series/title-series-A/ 使用 wordpress 插件缓存 1 天,但是当 缓存过期 并且访问者访问帖子系列
下面的示例访问者同时访问 url
- example.com/series/title-series-A/
- example.com/series/title-series-B/
- example.com/series/title-series-C/
所以所有被访问者访问的系列都会同时请求api,有什么解决方案吗?
(在这所有 posttype 'series' 都有 api php 请求)
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$agent = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'));
$api = 'https://api.example.com/v3/title-series-here';
$results = curl_get_contents($api, false, stream_context_create($agent));
$results = json_decode($results, true);
而且 api.example.com 对每个请求有 4 秒的速率限制,如果超过这个将被阻止
【问题讨论】:
-
只需添加一个 sleep(5);在每个请求之后。代码将“暂停”5秒
-
但是所有的post series请求会暂停5秒吗?还是只是 api 请求?因为发布a、b、c同时访问,同时暂停5秒?
标签: php json wordpress api file-get-contents