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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.