Jump to content

Cache_Lite error handling


upgrader

Recommended Posts

Hi there, I have this code to retrieve statistics from a page and cache them. The problem is when the site I am accessing goes down it just outputs a php error as expected. How can I rewrite this code so that if it doesn't manage to retrieve data from the site because it is down or it can't find the items it needs, it will display the last cached result instead?

 

require_once("Lite.php");

$objCache = new Cache_Lite( array("cacheDir" => "cache/", "lifeTime" =>
							300) );


if ($top3 = $objCache->get("top3")) 
{
	// Debug: $top3 .= "<li>[Cached Result]</li>";
	echo $top3;
}
else 
{		
	$html = file_get_contents('http://badhausen.hlstatsx.com/?mode=players&game=css'); 
	$dom = new domDocument;
	$dom->loadHTML($html);
	$dom->preserveWhiteSpace = false;

	$x = new DomXPath($dom);

	$results = $x->query('//table/tr[position() >= 2 and position() <= 4]/td[2]/font/a');

	foreach ($results as $result) {
		$top3 .= "<li>".$result->nodeValue."</li>\n";
		}
	echo $top3;

	$objCache->save($top3, "top3");
}

Link to comment
https://forums.phpfreaks.com/topic/162902-cache_lite-error-handling/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.