I have implemented a feature that uses Twitch’s API to determine whether a streamer is currently broadcasting or not. While the functionality works correctly, it’s creating major performance problems for my website. The page load time has increased dramatically to anywhere between 5 to 10 seconds whenever this check runs.
I’m wondering what approaches I could take to improve this situation. Would implementing caching mechanisms like cookies or session storage help? Are there other optimization strategies I should consider?
Here’s my current implementation:
public function checkStreamerStatus($channelName){
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://api.twitch.tv/kraken/streams/'.$channelName
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$searchTerm = "language";
$foundPos = strpos($result, $searchTerm);
curl_close($ch);
if ($foundPos === false) {
// Stream offline
} else {
if($channelName != null){
echo "streamActive";
}
}
}