Chris@0: Chris@0: * Chris@0: * This code is partially based on the Rack-Cache library by Ryan Tomayko, Chris@0: * which is released under the MIT license. Chris@0: * (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801) Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\HttpCache; Chris@0: Chris@17: use Symfony\Component\HttpFoundation\Request; Chris@17: use Symfony\Component\HttpFoundation\Response; Chris@0: use Symfony\Component\HttpKernel\HttpKernelInterface; Chris@0: use Symfony\Component\HttpKernel\TerminableInterface; Chris@0: Chris@0: /** Chris@0: * Cache provides HTTP caching. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class HttpCache implements HttpKernelInterface, TerminableInterface Chris@0: { Chris@0: private $kernel; Chris@0: private $store; Chris@0: private $request; Chris@0: private $surrogate; Chris@0: private $surrogateCacheStrategy; Chris@17: private $options = []; Chris@17: private $traces = []; Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * The available options are: Chris@0: * Chris@0: * * debug: If true, the traces are added as a HTTP header to ease debugging Chris@0: * Chris@0: * * default_ttl The number of seconds that a cache entry should be considered Chris@0: * fresh when no explicit freshness information is provided in Chris@0: * a response. Explicit Cache-Control or Expires headers Chris@0: * override this value. (default: 0) Chris@0: * Chris@0: * * private_headers Set of request headers that trigger "private" cache-control behavior Chris@0: * on responses that don't explicitly state whether the response is Chris@0: * public or private via a Cache-Control directive. (default: Authorization and Cookie) Chris@0: * Chris@0: * * allow_reload Specifies whether the client can force a cache reload by including a Chris@0: * Cache-Control "no-cache" directive in the request. Set it to ``true`` Chris@0: * for compliance with RFC 2616. (default: false) Chris@0: * Chris@0: * * allow_revalidate Specifies whether the client can force a cache revalidate by including Chris@0: * a Cache-Control "max-age=0" directive in the request. Set it to ``true`` Chris@0: * for compliance with RFC 2616. (default: false) Chris@0: * Chris@0: * * stale_while_revalidate Specifies the default number of seconds (the granularity is the second as the Chris@0: * Response TTL precision is a second) during which the cache can immediately return Chris@0: * a stale response while it revalidates it in the background (default: 2). Chris@0: * This setting is overridden by the stale-while-revalidate HTTP Cache-Control Chris@0: * extension (see RFC 5861). Chris@0: * Chris@0: * * stale_if_error Specifies the default number of seconds (the granularity is the second) during which Chris@0: * the cache can serve a stale response when an error is encountered (default: 60). Chris@0: * This setting is overridden by the stale-if-error HTTP Cache-Control extension Chris@0: * (see RFC 5861). Chris@0: */ Chris@17: public function __construct(HttpKernelInterface $kernel, StoreInterface $store, SurrogateInterface $surrogate = null, array $options = []) Chris@0: { Chris@0: $this->store = $store; Chris@0: $this->kernel = $kernel; Chris@0: $this->surrogate = $surrogate; Chris@0: Chris@0: // needed in case there is a fatal error because the backend is too slow to respond Chris@17: register_shutdown_function([$this->store, 'cleanup']); Chris@0: Chris@17: $this->options = array_merge([ Chris@0: 'debug' => false, Chris@0: 'default_ttl' => 0, Chris@17: 'private_headers' => ['Authorization', 'Cookie'], Chris@0: 'allow_reload' => false, Chris@0: 'allow_revalidate' => false, Chris@0: 'stale_while_revalidate' => 2, Chris@0: 'stale_if_error' => 60, Chris@17: ], $options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current store. Chris@0: * Chris@17: * @return StoreInterface A StoreInterface instance Chris@0: */ Chris@0: public function getStore() Chris@0: { Chris@0: return $this->store; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of events that took place during processing of the last request. Chris@0: * Chris@0: * @return array An array of events Chris@0: */ Chris@0: public function getTraces() Chris@0: { Chris@0: return $this->traces; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a log message for the events of the last request processing. Chris@0: * Chris@0: * @return string A log message Chris@0: */ Chris@0: public function getLog() Chris@0: { Chris@17: $log = []; Chris@0: foreach ($this->traces as $request => $traces) { Chris@0: $log[] = sprintf('%s: %s', $request, implode(', ', $traces)); Chris@0: } Chris@0: Chris@0: return implode('; ', $log); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the Request instance associated with the master request. Chris@0: * Chris@0: * @return Request A Request instance Chris@0: */ Chris@0: public function getRequest() Chris@0: { Chris@0: return $this->request; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the Kernel instance. Chris@0: * Chris@0: * @return HttpKernelInterface An HttpKernelInterface instance Chris@0: */ Chris@0: public function getKernel() Chris@0: { Chris@0: return $this->kernel; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the Surrogate instance. Chris@0: * Chris@0: * @return SurrogateInterface A Surrogate instance Chris@0: * Chris@0: * @throws \LogicException Chris@0: */ Chris@0: public function getSurrogate() Chris@0: { Chris@0: return $this->surrogate; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) Chris@0: { Chris@0: // FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism Chris@0: if (HttpKernelInterface::MASTER_REQUEST === $type) { Chris@17: $this->traces = []; Chris@16: // Keep a clone of the original request for surrogates so they can access it. Chris@16: // We must clone here to get a separate instance because the application will modify the request during Chris@16: // the application flow (we know it always does because we do ourselves by setting REMOTE_ADDR to 127.0.0.1 Chris@16: // and adding the X-Forwarded-For header, see HttpCache::forward()). Chris@16: $this->request = clone $request; Chris@0: if (null !== $this->surrogate) { Chris@0: $this->surrogateCacheStrategy = $this->surrogate->createCacheStrategy(); Chris@0: } Chris@0: } Chris@0: Chris@17: $this->traces[$this->getTraceKey($request)] = []; Chris@0: Chris@0: if (!$request->isMethodSafe(false)) { Chris@0: $response = $this->invalidate($request, $catch); Chris@0: } elseif ($request->headers->has('expect') || !$request->isMethodCacheable()) { Chris@0: $response = $this->pass($request, $catch); Chris@14: } elseif ($this->options['allow_reload'] && $request->isNoCache()) { Chris@14: /* Chris@14: If allow_reload is configured and the client requests "Cache-Control: no-cache", Chris@14: reload the cache by fetching a fresh response and caching it (if possible). Chris@14: */ Chris@14: $this->record($request, 'reload'); Chris@14: $response = $this->fetch($request, $catch); Chris@0: } else { Chris@0: $response = $this->lookup($request, $catch); Chris@0: } Chris@0: Chris@0: $this->restoreResponseBody($request, $response); Chris@0: Chris@0: if (HttpKernelInterface::MASTER_REQUEST === $type && $this->options['debug']) { Chris@0: $response->headers->set('X-Symfony-Cache', $this->getLog()); Chris@0: } Chris@0: Chris@0: if (null !== $this->surrogate) { Chris@0: if (HttpKernelInterface::MASTER_REQUEST === $type) { Chris@0: $this->surrogateCacheStrategy->update($response); Chris@0: } else { Chris@0: $this->surrogateCacheStrategy->add($response); Chris@0: } Chris@0: } Chris@0: Chris@0: $response->prepare($request); Chris@0: Chris@0: $response->isNotModified($request); Chris@0: Chris@0: return $response; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function terminate(Request $request, Response $response) Chris@0: { Chris@0: if ($this->getKernel() instanceof TerminableInterface) { Chris@0: $this->getKernel()->terminate($request, $response); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Forwards the Request to the backend without storing the Response in the cache. Chris@0: * Chris@0: * @param Request $request A Request instance Chris@0: * @param bool $catch Whether to process exceptions Chris@0: * Chris@0: * @return Response A Response instance Chris@0: */ Chris@0: protected function pass(Request $request, $catch = false) Chris@0: { Chris@0: $this->record($request, 'pass'); Chris@0: Chris@0: return $this->forward($request, $catch); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Invalidates non-safe methods (like POST, PUT, and DELETE). Chris@0: * Chris@0: * @param Request $request A Request instance Chris@0: * @param bool $catch Whether to process exceptions Chris@0: * Chris@0: * @return Response A Response instance Chris@0: * Chris@0: * @throws \Exception Chris@0: * Chris@0: * @see RFC2616 13.10 Chris@0: */ Chris@0: protected function invalidate(Request $request, $catch = false) Chris@0: { Chris@0: $response = $this->pass($request, $catch); Chris@0: Chris@0: // invalidate only when the response is successful Chris@0: if ($response->isSuccessful() || $response->isRedirect()) { Chris@0: try { Chris@0: $this->store->invalidate($request); Chris@0: Chris@0: // As per the RFC, invalidate Location and Content-Location URLs if present Chris@17: foreach (['Location', 'Content-Location'] as $header) { Chris@0: if ($uri = $response->headers->get($header)) { Chris@17: $subRequest = Request::create($uri, 'get', [], [], [], $request->server->all()); Chris@0: Chris@0: $this->store->invalidate($subRequest); Chris@0: } Chris@0: } Chris@0: Chris@0: $this->record($request, 'invalidate'); Chris@0: } catch (\Exception $e) { Chris@0: $this->record($request, 'invalidate-failed'); Chris@0: Chris@0: if ($this->options['debug']) { Chris@0: throw $e; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $response; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Lookups a Response from the cache for the given Request. Chris@0: * Chris@0: * When a matching cache entry is found and is fresh, it uses it as the Chris@0: * response without forwarding any request to the backend. When a matching Chris@0: * cache entry is found but is stale, it attempts to "validate" the entry with Chris@0: * the backend using conditional GET. When no matching cache entry is found, Chris@0: * it triggers "miss" processing. Chris@0: * Chris@0: * @param Request $request A Request instance Chris@14: * @param bool $catch Whether to process exceptions Chris@0: * Chris@0: * @return Response A Response instance Chris@0: * Chris@0: * @throws \Exception Chris@0: */ Chris@0: protected function lookup(Request $request, $catch = false) Chris@0: { Chris@0: try { Chris@0: $entry = $this->store->lookup($request); Chris@0: } catch (\Exception $e) { Chris@0: $this->record($request, 'lookup-failed'); Chris@0: Chris@0: if ($this->options['debug']) { Chris@0: throw $e; Chris@0: } Chris@0: Chris@0: return $this->pass($request, $catch); Chris@0: } Chris@0: Chris@0: if (null === $entry) { Chris@0: $this->record($request, 'miss'); Chris@0: Chris@0: return $this->fetch($request, $catch); Chris@0: } Chris@0: Chris@0: if (!$this->isFreshEnough($request, $entry)) { Chris@0: $this->record($request, 'stale'); Chris@0: Chris@0: return $this->validate($request, $entry, $catch); Chris@0: } Chris@0: Chris@0: $this->record($request, 'fresh'); Chris@0: Chris@0: $entry->headers->set('Age', $entry->getAge()); Chris@0: Chris@0: return $entry; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validates that a cache entry is fresh. Chris@0: * Chris@0: * The original request is used as a template for a conditional Chris@0: * GET request with the backend. Chris@0: * Chris@0: * @param Request $request A Request instance Chris@0: * @param Response $entry A Response instance to validate Chris@0: * @param bool $catch Whether to process exceptions Chris@0: * Chris@0: * @return Response A Response instance Chris@0: */ Chris@0: protected function validate(Request $request, Response $entry, $catch = false) Chris@0: { Chris@0: $subRequest = clone $request; Chris@0: Chris@0: // send no head requests because we want content Chris@0: if ('HEAD' === $request->getMethod()) { Chris@0: $subRequest->setMethod('GET'); Chris@0: } Chris@0: Chris@0: // add our cached last-modified validator Chris@0: $subRequest->headers->set('if_modified_since', $entry->headers->get('Last-Modified')); Chris@0: Chris@0: // Add our cached etag validator to the environment. Chris@0: // We keep the etags from the client to handle the case when the client Chris@0: // has a different private valid entry which is not cached here. Chris@17: $cachedEtags = $entry->getEtag() ? [$entry->getEtag()] : []; Chris@0: $requestEtags = $request->getETags(); Chris@0: if ($etags = array_unique(array_merge($cachedEtags, $requestEtags))) { Chris@0: $subRequest->headers->set('if_none_match', implode(', ', $etags)); Chris@0: } Chris@0: Chris@0: $response = $this->forward($subRequest, $catch, $entry); Chris@0: Chris@0: if (304 == $response->getStatusCode()) { Chris@0: $this->record($request, 'valid'); Chris@0: Chris@0: // return the response and not the cache entry if the response is valid but not cached Chris@0: $etag = $response->getEtag(); Chris@17: if ($etag && \in_array($etag, $requestEtags) && !\in_array($etag, $cachedEtags)) { Chris@0: return $response; Chris@0: } Chris@0: Chris@0: $entry = clone $entry; Chris@0: $entry->headers->remove('Date'); Chris@0: Chris@17: foreach (['Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified'] as $name) { Chris@0: if ($response->headers->has($name)) { Chris@0: $entry->headers->set($name, $response->headers->get($name)); Chris@0: } Chris@0: } Chris@0: Chris@0: $response = $entry; Chris@0: } else { Chris@0: $this->record($request, 'invalid'); Chris@0: } Chris@0: Chris@0: if ($response->isCacheable()) { Chris@0: $this->store($request, $response); Chris@0: } Chris@0: Chris@0: return $response; Chris@0: } Chris@0: Chris@0: /** Chris@14: * Unconditionally fetches a fresh response from the backend and Chris@14: * stores it in the cache if is cacheable. Chris@0: * Chris@0: * @param Request $request A Request instance Chris@14: * @param bool $catch Whether to process exceptions Chris@0: * Chris@0: * @return Response A Response instance Chris@0: */ Chris@0: protected function fetch(Request $request, $catch = false) Chris@0: { Chris@0: $subRequest = clone $request; Chris@0: Chris@0: // send no head requests because we want content Chris@0: if ('HEAD' === $request->getMethod()) { Chris@0: $subRequest->setMethod('GET'); Chris@0: } Chris@0: Chris@0: // avoid that the backend sends no content Chris@0: $subRequest->headers->remove('if_modified_since'); Chris@0: $subRequest->headers->remove('if_none_match'); Chris@0: Chris@0: $response = $this->forward($subRequest, $catch); Chris@0: Chris@0: if ($response->isCacheable()) { Chris@0: $this->store($request, $response); Chris@0: } Chris@0: Chris@0: return $response; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Forwards the Request to the backend and returns the Response. Chris@0: * Chris@14: * All backend requests (cache passes, fetches, cache validations) Chris@14: * run through this method. Chris@14: * Chris@0: * @param Request $request A Request instance Chris@0: * @param bool $catch Whether to catch exceptions or not Chris@0: * @param Response $entry A Response instance (the stale entry if present, null otherwise) Chris@0: * Chris@0: * @return Response A Response instance Chris@0: */ Chris@0: protected function forward(Request $request, $catch = false, Response $entry = null) Chris@0: { Chris@0: if ($this->surrogate) { Chris@0: $this->surrogate->addSurrogateCapability($request); Chris@0: } Chris@0: Chris@0: // always a "master" request (as the real master request can be in cache) Chris@17: $response = SubRequestHandler::handle($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, $catch); Chris@0: Chris@0: // we don't implement the stale-if-error on Requests, which is nonetheless part of the RFC Chris@17: if (null !== $entry && \in_array($response->getStatusCode(), [500, 502, 503, 504])) { Chris@0: if (null === $age = $entry->headers->getCacheControlDirective('stale-if-error')) { Chris@0: $age = $this->options['stale_if_error']; Chris@0: } Chris@0: Chris@0: if (abs($entry->getTtl()) < $age) { Chris@0: $this->record($request, 'stale-if-error'); Chris@0: Chris@0: return $entry; Chris@0: } Chris@0: } Chris@0: Chris@14: /* Chris@14: RFC 7231 Sect. 7.1.1.2 says that a server that does not have a reasonably accurate Chris@14: clock MUST NOT send a "Date" header, although it MUST send one in most other cases Chris@14: except for 1xx or 5xx responses where it MAY do so. Chris@14: Chris@14: Anyway, a client that received a message without a "Date" header MUST add it. Chris@14: */ Chris@14: if (!$response->headers->has('Date')) { Chris@14: $response->setDate(\DateTime::createFromFormat('U', time())); Chris@14: } Chris@14: Chris@0: $this->processResponseBody($request, $response); Chris@0: Chris@0: if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) { Chris@0: $response->setPrivate(); Chris@0: } elseif ($this->options['default_ttl'] > 0 && null === $response->getTtl() && !$response->headers->getCacheControlDirective('must-revalidate')) { Chris@0: $response->setTtl($this->options['default_ttl']); Chris@0: } Chris@0: Chris@0: return $response; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether the cache entry is "fresh enough" to satisfy the Request. Chris@0: * Chris@0: * @return bool true if the cache entry if fresh enough, false otherwise Chris@0: */ Chris@0: protected function isFreshEnough(Request $request, Response $entry) Chris@0: { Chris@0: if (!$entry->isFresh()) { Chris@0: return $this->lock($request, $entry); Chris@0: } Chris@0: Chris@0: if ($this->options['allow_revalidate'] && null !== $maxAge = $request->headers->getCacheControlDirective('max-age')) { Chris@0: return $maxAge > 0 && $maxAge >= $entry->getAge(); Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Locks a Request during the call to the backend. Chris@0: * Chris@0: * @return bool true if the cache entry can be returned even if it is staled, false otherwise Chris@0: */ Chris@0: protected function lock(Request $request, Response $entry) Chris@0: { Chris@0: // try to acquire a lock to call the backend Chris@0: $lock = $this->store->lock($request); Chris@0: Chris@14: if (true === $lock) { Chris@14: // we have the lock, call the backend Chris@14: return false; Chris@14: } Chris@14: Chris@0: // there is already another process calling the backend Chris@0: Chris@14: // May we serve a stale response? Chris@14: if ($this->mayServeStaleWhileRevalidate($entry)) { Chris@14: $this->record($request, 'stale-while-revalidate'); Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@14: // wait for the lock to be released Chris@14: if ($this->waitForLock($request)) { Chris@14: // replace the current entry with the fresh one Chris@14: $new = $this->lookup($request); Chris@14: $entry->headers = $new->headers; Chris@14: $entry->setContent($new->getContent()); Chris@14: $entry->setStatusCode($new->getStatusCode()); Chris@14: $entry->setProtocolVersion($new->getProtocolVersion()); Chris@14: foreach ($new->headers->getCookies() as $cookie) { Chris@14: $entry->headers->setCookie($cookie); Chris@14: } Chris@14: } else { Chris@14: // backend is slow as hell, send a 503 response (to avoid the dog pile effect) Chris@14: $entry->setStatusCode(503); Chris@14: $entry->setContent('503 Service Unavailable'); Chris@14: $entry->headers->set('Retry-After', 10); Chris@14: } Chris@14: Chris@14: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes the Response to the cache. Chris@0: * Chris@0: * @throws \Exception Chris@0: */ Chris@0: protected function store(Request $request, Response $response) Chris@0: { Chris@0: try { Chris@0: $this->store->write($request, $response); Chris@0: Chris@0: $this->record($request, 'store'); Chris@0: Chris@0: $response->headers->set('Age', $response->getAge()); Chris@0: } catch (\Exception $e) { Chris@0: $this->record($request, 'store-failed'); Chris@0: Chris@0: if ($this->options['debug']) { Chris@0: throw $e; Chris@0: } Chris@0: } Chris@0: Chris@0: // now that the response is cached, release the lock Chris@0: $this->store->unlock($request); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Restores the Response body. Chris@0: */ Chris@0: private function restoreResponseBody(Request $request, Response $response) Chris@0: { Chris@0: if ($response->headers->has('X-Body-Eval')) { Chris@0: ob_start(); Chris@0: Chris@0: if ($response->headers->has('X-Body-File')) { Chris@0: include $response->headers->get('X-Body-File'); Chris@0: } else { Chris@0: eval('; ?>'.$response->getContent().'setContent(ob_get_clean()); Chris@0: $response->headers->remove('X-Body-Eval'); Chris@0: if (!$response->headers->has('Transfer-Encoding')) { Chris@17: $response->headers->set('Content-Length', \strlen($response->getContent())); Chris@0: } Chris@0: } elseif ($response->headers->has('X-Body-File')) { Chris@14: // Response does not include possibly dynamic content (ESI, SSI), so we need Chris@14: // not handle the content for HEAD requests Chris@14: if (!$request->isMethod('HEAD')) { Chris@14: $response->setContent(file_get_contents($response->headers->get('X-Body-File'))); Chris@14: } Chris@0: } else { Chris@0: return; Chris@0: } Chris@0: Chris@0: $response->headers->remove('X-Body-File'); Chris@0: } Chris@0: Chris@0: protected function processResponseBody(Request $request, Response $response) Chris@0: { Chris@0: if (null !== $this->surrogate && $this->surrogate->needsParsing($response)) { Chris@0: $this->surrogate->process($request, $response); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if the Request includes authorization or other sensitive information Chris@0: * that should cause the Response to be considered private by default. Chris@0: * Chris@0: * @return bool true if the Request is private, false otherwise Chris@0: */ Chris@0: private function isPrivateRequest(Request $request) Chris@0: { Chris@0: foreach ($this->options['private_headers'] as $key) { Chris@0: $key = strtolower(str_replace('HTTP_', '', $key)); Chris@0: Chris@0: if ('cookie' === $key) { Chris@17: if (\count($request->cookies->all())) { Chris@0: return true; Chris@0: } Chris@0: } elseif ($request->headers->has($key)) { Chris@0: return true; Chris@0: } Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Records that an event took place. Chris@0: * Chris@0: * @param Request $request A Request instance Chris@0: * @param string $event The event name Chris@0: */ Chris@0: private function record(Request $request, $event) Chris@0: { Chris@14: $this->traces[$this->getTraceKey($request)][] = $event; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Calculates the key we use in the "trace" array for a given request. Chris@14: * Chris@14: * @param Request $request Chris@14: * Chris@14: * @return string Chris@14: */ Chris@14: private function getTraceKey(Request $request) Chris@14: { Chris@0: $path = $request->getPathInfo(); Chris@0: if ($qs = $request->getQueryString()) { Chris@0: $path .= '?'.$qs; Chris@0: } Chris@14: Chris@14: return $request->getMethod().' '.$path; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Checks whether the given (cached) response may be served as "stale" when a revalidation Chris@14: * is currently in progress. Chris@14: * Chris@14: * @param Response $entry Chris@14: * Chris@14: * @return bool true when the stale response may be served, false otherwise Chris@14: */ Chris@14: private function mayServeStaleWhileRevalidate(Response $entry) Chris@14: { Chris@14: $timeout = $entry->headers->getCacheControlDirective('stale-while-revalidate'); Chris@14: Chris@14: if (null === $timeout) { Chris@14: $timeout = $this->options['stale_while_revalidate']; Chris@14: } Chris@14: Chris@14: return abs($entry->getTtl()) < $timeout; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Waits for the store to release a locked entry. Chris@14: * Chris@14: * @param Request $request The request to wait for Chris@14: * Chris@14: * @return bool true if the lock was released before the internal timeout was hit; false if the wait timeout was exceeded Chris@14: */ Chris@14: private function waitForLock(Request $request) Chris@14: { Chris@14: $wait = 0; Chris@14: while ($this->store->isLocked($request) && $wait < 100) { Chris@14: usleep(50000); Chris@14: ++$wait; Chris@14: } Chris@14: Chris@14: return $wait < 100; Chris@0: } Chris@0: }