upgrader Posted June 19, 2009 Share Posted June 19, 2009 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/162902-cache_lite-error-handling/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.