public function loadUrl($url, array $options = null) { $co = new CacheObject($url, CacheObject::CO_COMPRESS | CacheObject::CO_USE_DISK, function () use($url) { $http = new HttpRequest(); $http->open("GET", $url); \debug("RssReader: Loading RSS from {$url}"); if ($http->send()) { return $http->getResponseText(); } }); $this->loadString($co->getContent()); }
private function execute() { if ($this->status !== null) { return null; } // Check cache policy if ($this->options[self::OPT_CACHEPOLICY] == self::CACHEPOLICY_AUTO) { $use_cache = true; } else { $use_cache = false; } // Don't cache post requests if ($this->client->getMethod() == "POST") { $use_cache = false; } $this->client->setUrl($this->url); if ($use_cache) { $flags = CacheObject::CO_USE_DISK | CacheObject::CO_COMPRESS; $co = new CacheObject($this->url, $flags, function ($url) { $this->debug("Refreshing cache object..."); $status = $this->client->execute(); $this->debug("Response gave status {$status} when updating cache object"); //if ($status == 200) { $doc = $this->client->getResponse(); $ct = $this->client->getContentType(); // Calculate expiry here return [$doc, $ct, '30m']; //} //return null; }); if ($co->isCached()) { $this->debug("Returning content from cache"); $this->emit(self::ON_CACHEHIT); } $this->response = $co->getContent(); $this->contenttype = $co->getContentType(); $this->status = 200; } else { $this->debug("Sending direct request"); $this->status = $this->client->execute(); $this->response = $this->client->getResponse(); $this->headers = $this->client->getAllHeaders(); $this->contenttype = null; // $this->client->getHeaders(); } if ($this->status == 200) { return true; } return false; }
public static function getUrl($url, $refresh = false) { $flags = self::CO_USE_DISK | self::CO_COMPRESS; if ($refresh) { $flags |= self::CO_FLUSH; } $co = new CacheObject($url, $flags, function ($assetid, $var) { $doc = file_get_contents($assetid); return [$doc, 'text/html', '30m']; }); return $co->getContent(); }