/** * Calls the corresponding function in the block and executes the data retrieval function. * @return \SimpleXMLElement The XML data tree */ public final function callMemcacheBlock() { $callback = $this->getCallback(); $db = $this->getDatabase(); $mem = new \System\Cache\Memcache\Memcache(); $xml = null; if ($mem->keyExists($this->key)) { $xmlString = $mem->get($this->key); if ($xmlString) { //suppress warning here in case of malformed xml $xml = @simplexml_load_string($xmlString); if ($xml instanceof \SimpleXMLElement) { return $xml; } else { $errorLogger = \System\Log\ErrorLogger::getInstance(); $errorLogger->out('[MemcacheBlock] Could not read memcache key ' . $this->key . ' as XML. Regenerating.', \System\Log\LoggerLevel::LEVEL_WARNING); } } } if (is_callable($callback)) { $xml = call_user_func($callback, $this); $mem->store($this->key, $xml->asXML(), $this->timeout); } else { throw new \System\Error\Exception\InvalidMethodException('The given callback cannot be called. Does it exist and is it public?'); } return $xml; }
/** * Calls the corresponding function in the block and checks for LUT availability. * @return \SimpleXMLElement The XML data tree */ public final function callStaticBlock() { $db = $this->getDatabase(); $xml = ''; $filename = null; $returnVal = \System\Cache\LUTCache\LUTCache::retrieve($db, $this->getLUTKey(), $filename); switch ($returnVal) { case \System\Cache\LUTCache\Status::CACHE_GENERATING: $totalWaitTime = 0; while (($returnVal = \System\Cache\LUTCache\LUTCache::retrieve($db, $this->getLUTKey(), $filename)) == \System\Cache\LUTCache\Status::CACHE_GENERATING) { //we wait before polling the system again usleep(self::CACHE_GENERATE_POLL_TIME); $totalWaitTime += self::CACHE_GENERATE_POLL_TIME; if ($totalWaitTime >= self::CACHE_GENERATE_WAIT_THRESHOLD) { throw new \System\Error\Exception\LUTCacheTimeoutException('The generation timeout is exceeded for block: ' . $this->getLUTKey()); } } if ($returnVal != \System\Cache\LUTCache\Status::CACHE_HIT) { throw new \Exception('Invalid LUT returnvalue given after generation: ' . $returnVal); } //we do a fallthrough after we are done generating //we do a fallthrough after we are done generating case \System\Cache\LUTCache\Status::CACHE_HIT: //get stuff $fullFile = self::getWritablePath($filename) . $filename; if (file_exists($fullFile)) { //suppress warnings here in case of malformed xml if ($xml = @simplexml_load_file($fullFile)) { break; } else { $errorLogger = \System\Log\ErrorLogger::getInstance(); $errorLogger->out('[StaticBlock] Could not read ' . $fullFile . ' as XML. Regenerating.', \System\Log\LoggerLevel::LEVEL_WARNING); } } //if the file does not exist, we do a fallthrough to the generation //if the file does not exist, we do a fallthrough to the generation case \System\Cache\LUTCache\Status::CACHE_MISS: case \System\Cache\LUTCache\Status::CACHE_INVALIDATED: //set to currently generating \System\Cache\LUTCache\LUTCache::setToGenerate($db, $this->getLUTKey()); //get the XML $xml = $this->callBlock($this); //store the xml to a file and add the file to the LUT $cacheFile = self::FILENAME_PREFIX . uniqid() . self::FILENAME_EXTENSION; $fullFile = self::getWritablePath($cacheFile) . $cacheFile; $xml->asXML($fullFile); \System\Cache\LUTCache\LUTCache::store($db, $this->getLUTKey(), $cacheFile); break; default: throw new \Exception('Invalid LUT returnvalue given: ' . $returnVal); } return $xml; }