[PHP] Velocizzare il tuo sito utilizzando la cache!

Posted on 10. Oct, 2009 by daniele in php, script php, tutorials


Un modo molto semplice per accelerare notevolmente il tuo sito web č di recuperare le informazioni in remoto da altri siti web o database. Ecco un esempio molto completo che utilizza le funzioni curl di PHP!

 
/* settings */
$cache_path = '/cache/';
$rss_file_name = date('Y-m-d').'-rss.txt';
 
/* rss */
if(file_exists($cache_path.$rss_file_name)) {
	$rss_subcribers = file_get_contents($cache_path.$rss_file_name);
}
else {
	$rss_content = get_url('https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=dfadajf324789fhSDFDf48');
	$subscribers = get_match('/circulation="(.*)"/isU',$rss_content);
	if($subscribers) {
		$subscribers = number_format($subscribers,0,'',',');
		file_put_contents($cache_path.$rss_file_name,$subscribers);
		$rss_subcribers = $subscribers;
	}
}
 
/* display */
echo 'My subscriber count is: ',$rss_subscribers;
 
/* connects to the URL, returns content */
function get_url($url) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
	$content = curl_exec($ch);
	curl_close($ch);
	return $content;
}
 
/* helper: does the regex */
function get_match($regex,$content) {
	preg_match($regex,$content,$matches);
	return $matches[1];
}
 


Correlati

Leave a reply