/** * Returns an object modelling the search results from the specified * URL (which is expected to be JSON). * * Just a wrapper to avoid cURL and cache boilerplate elsewhere. */ public static function getSearchFromJson($url) { $search = function () use($url) { // humbly submitted: this is a stupid interface. $ch = curl_init($url); curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, \CURLOPT_HEADER, 0); $result = curl_exec($ch); curl_close($ch); return json_decode($result); }; // If we've got a memcache wrapper available, store the results // for a bit. // TODO: Offer a filesystem-based alternative. if (class_exists('\\SparkCache')) { $cachekey = get_called_class() . md5($url); $cache = \SparkCache::getInstance(); return $cache->getOrSet($cachekey, $search); } else { return $search(); } }
/** * Invalidate the memcache entry for this record. */ public function invalidateCache() { if (!MEMCACHED_ENABLED === true) { return; } return SparkCache::getInstance()->delete($this->cacheKey()); }