/** * Cleans the current file cache by deleting unused files. * @param \System\Db\Database The database to LUT query */ public static final function cleanPageCache(\System\Db\Database $db) { $results = \System\Cache\LUTCache\LUTCache::getCache($db); $baseFiles = new \System\Collection\Vector(); foreach ($results as $result) { $baseFiles[] = basename($result->value); } $folder = new \System\IO\Directory(\System\Cache\PageCache\StaticBlock::CACHE_CACHEFOLDER); $extensions = new \System\Collection\Vector('xml'); $files = $folder->getFiles($extensions); foreach ($files as $file) { /** @var \System\IO\File */ $file = $file; if (mb_strpos($file->getFilename(), 'PageCache') !== false) { if (!$baseFiles->contains($file->getFilename())) { $file->delete(); } } } }
/** * 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; }