function testParseCacheTime() { $client = new \arc\http\ClientStream(); $headers = ['Expires: ' . gmdate('D, d M Y H:i:s T', time() + 100)]; date_default_timezone_set("UTC"); $cachetime = \arc\http\headers::parseCacheTime($headers); $this->assertTrue($cachetime > 0); }
function run() { $client = \arc\cache::proxy(\arc\http::client(), function ($params) { return \arc\http\headers::parseCacheTime($params['target']->responseHeaders); }); $feeds = handleFeeds(__DIR__ . '/../data/feeds.json', $client); $aggregator = aggregator($client); echo h::doctype() . h::html(h::head(h::title('RSS Aggregator'), h::link(['href' => '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css', 'rel' => 'stylesheet']), h::link(['href' => 'cards.css', 'rel' => 'stylesheet']), h::script(['src' => '//code.jquery.com/jquery-1.12.0.min.js']), h::script(['src' => 'cards.js'])), h::body(h::raw($aggregator->render($feeds)), h::footer(h::form(['method' => 'POST'], h::input(['name' => 'newFeed']), h::button(['ype' => 'submit'], 'Add feed'))))); }
function testCacheMultiple() { $headerString = <<<EOF HTTP/1.1 200 OK Cache-Control: public Cache-Control: max-age=300,s-maxage=900 EOF; $headers = \arc\http\headers::parse($headerString); $cachetime = \arc\http\headers::parseCacheTime($headers); $this->assertEquals(300, $cachetime); }
<?php require __DIR__ . '/../vendor/autoload.php'; use arc\html as h; use arc\xml as x; $client = \arc\cache::proxy(\arc\http::client(), function ($params) { return \arc\http\headers::parseCacheTime($params['target']->responseHeaders); }); $feed = $client->get('https://www.nasa.gov/rss/dyn/breaking_news.rss'); try { $rss = x::parse($feed); } catch (\Exception $e) { $rss = h::parse($feed); } $items = $rss->find('item'); foreach ($items as $item) { echo h::article(h::h2(h::a(['href' => $item->link->nodeValue], $item->title->nodeValue)), h::div(['class' => 'body'], $item->description->nodeValue)); }
/** * Parse response headers to determine if and how long you may cache the response. Doesn't understand ETags. * @param string|string[] $headers Headers string or array as returned by parse() * @param bool $private Whether to store a private cache or public cache image. * @return int The number of seconds you may cache this result starting from now. */ public static function parseCacheTime($headers, $private = true) { $result = null; if (is_string($headers) || !isset($headers['Cache-Control']) && !isset($headers['Expires'])) { $headers = \arc\http\headers::parse($headers); } if (isset($headers['Cache-Control'])) { $header = self::mergeHeaders($headers['Cache-Control']); $result = self::getCacheControlTime($header, $private); } if (!isset($result) && isset($headers['Expires'])) { $result = strtotime(self::getLastHeader($headers['Expires'])) - time(); } return (int) $result; }