private function setRequestCache(RemoteContentRequest $originalRequest, RemoteContentRequest $request, Cache $cache) { if ($request->isStrictNoCache()) { return; } $ignoreCache = $originalRequest->getOptions()->ignoreCache; if (($this->cachePostRequest || !$request->isPost()) && !$ignoreCache) { $ttl = Shindig_Config::get('cache_time'); if ((int) $request->getHttpCode() == 200) { // Got a 200 OK response, calculate the TTL to use for caching it if (($expires = $request->getResponseHeader('Expires')) != null) { // prefer to use the servers notion of the time since there could be a clock-skew, but otherwise use our own $date = $request->getResponseHeader('Date') != null ? $request->getResponseHeader('Date') : gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME']) . ' GMT'; // convert both dates to unix epoch seconds, and calculate the TTL date_default_timezone_set('GMT'); $date = strtotime($date); $expires = strtotime($expires); $ttl = $expires - $date; // Don't fall for the old expiration-date-in-the-past trick, we *really* want to cache stuff since a large SNS's traffic would devastate a gadget developer's server if ($expires - $date > 1) { $ttl = $expires - $date; } } // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html : The Cache-Control: max-age=<seconds> overrides the expires header, so if both are present this one will overwrite the $ttl if (($cacheControl = $request->getResponseHeader('Cache-Control')) != null) { $bits = explode('=', $cacheControl); foreach ($bits as $key => $val) { if ($val == 'max-age' && isset($bits[$key + 1])) { $ttl = $bits[$key + 1]; break; } } } } else { $ttl = 5 * 60; // cache errors for 5 minutes, takes the denial of service attack type behaviour out of having an error :) } $this->invalidateService->markResponse($request); $this->cache->set($originalRequest->toHash(), $request, $ttl); } }