Без опису

HttpCache.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /*
  11. * This code is partially based on the Rack-Cache library by Ryan Tomayko,
  12. * which is released under the MIT license.
  13. * (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
  14. */
  15. namespace Symfony\Component\HttpKernel\HttpCache;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\HttpKernelInterface;
  19. use Symfony\Component\HttpKernel\TerminableInterface;
  20. /**
  21. * Cache provides HTTP caching.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class HttpCache implements HttpKernelInterface, TerminableInterface
  26. {
  27. private $kernel;
  28. private $store;
  29. private $request;
  30. private $surrogate;
  31. private $surrogateCacheStrategy;
  32. private $options = [];
  33. private $traces = [];
  34. /**
  35. * Constructor.
  36. *
  37. * The available options are:
  38. *
  39. * * debug If true, exceptions are thrown when things go wrong. Otherwise, the cache
  40. * will try to carry on and deliver a meaningful response.
  41. *
  42. * * trace_level May be one of 'none', 'short' and 'full'. For 'short', a concise trace of the
  43. * master request will be added as an HTTP header. 'full' will add traces for all
  44. * requests (including ESI subrequests). (default: 'full' if in debug; 'none' otherwise)
  45. *
  46. * * trace_header Header name to use for traces. (default: X-Symfony-Cache)
  47. *
  48. * * default_ttl The number of seconds that a cache entry should be considered
  49. * fresh when no explicit freshness information is provided in
  50. * a response. Explicit Cache-Control or Expires headers
  51. * override this value. (default: 0)
  52. *
  53. * * private_headers Set of request headers that trigger "private" cache-control behavior
  54. * on responses that don't explicitly state whether the response is
  55. * public or private via a Cache-Control directive. (default: Authorization and Cookie)
  56. *
  57. * * allow_reload Specifies whether the client can force a cache reload by including a
  58. * Cache-Control "no-cache" directive in the request. Set it to ``true``
  59. * for compliance with RFC 2616. (default: false)
  60. *
  61. * * allow_revalidate Specifies whether the client can force a cache revalidate by including
  62. * a Cache-Control "max-age=0" directive in the request. Set it to ``true``
  63. * for compliance with RFC 2616. (default: false)
  64. *
  65. * * stale_while_revalidate Specifies the default number of seconds (the granularity is the second as the
  66. * Response TTL precision is a second) during which the cache can immediately return
  67. * a stale response while it revalidates it in the background (default: 2).
  68. * This setting is overridden by the stale-while-revalidate HTTP Cache-Control
  69. * extension (see RFC 5861).
  70. *
  71. * * stale_if_error Specifies the default number of seconds (the granularity is the second) during which
  72. * the cache can serve a stale response when an error is encountered (default: 60).
  73. * This setting is overridden by the stale-if-error HTTP Cache-Control extension
  74. * (see RFC 5861).
  75. */
  76. public function __construct(HttpKernelInterface $kernel, StoreInterface $store, SurrogateInterface $surrogate = null, array $options = [])
  77. {
  78. $this->store = $store;
  79. $this->kernel = $kernel;
  80. $this->surrogate = $surrogate;
  81. // needed in case there is a fatal error because the backend is too slow to respond
  82. register_shutdown_function([$this->store, 'cleanup']);
  83. $this->options = array_merge([
  84. 'debug' => false,
  85. 'default_ttl' => 0,
  86. 'private_headers' => ['Authorization', 'Cookie'],
  87. 'allow_reload' => false,
  88. 'allow_revalidate' => false,
  89. 'stale_while_revalidate' => 2,
  90. 'stale_if_error' => 60,
  91. 'trace_level' => 'none',
  92. 'trace_header' => 'X-Symfony-Cache',
  93. ], $options);
  94. if (!isset($options['trace_level'])) {
  95. $this->options['trace_level'] = $this->options['debug'] ? 'full' : 'none';
  96. }
  97. }
  98. /**
  99. * Gets the current store.
  100. *
  101. * @return StoreInterface A StoreInterface instance
  102. */
  103. public function getStore()
  104. {
  105. return $this->store;
  106. }
  107. /**
  108. * Returns an array of events that took place during processing of the last request.
  109. *
  110. * @return array An array of events
  111. */
  112. public function getTraces()
  113. {
  114. return $this->traces;
  115. }
  116. private function addTraces(Response $response)
  117. {
  118. $traceString = null;
  119. if ('full' === $this->options['trace_level']) {
  120. $traceString = $this->getLog();
  121. }
  122. if ('short' === $this->options['trace_level'] && $masterId = array_key_first($this->traces)) {
  123. $traceString = implode('/', $this->traces[$masterId]);
  124. }
  125. if (null !== $traceString) {
  126. $response->headers->add([$this->options['trace_header'] => $traceString]);
  127. }
  128. }
  129. /**
  130. * Returns a log message for the events of the last request processing.
  131. *
  132. * @return string A log message
  133. */
  134. public function getLog()
  135. {
  136. $log = [];
  137. foreach ($this->traces as $request => $traces) {
  138. $log[] = sprintf('%s: %s', $request, implode(', ', $traces));
  139. }
  140. return implode('; ', $log);
  141. }
  142. /**
  143. * Gets the Request instance associated with the master request.
  144. *
  145. * @return Request A Request instance
  146. */
  147. public function getRequest()
  148. {
  149. return $this->request;
  150. }
  151. /**
  152. * Gets the Kernel instance.
  153. *
  154. * @return HttpKernelInterface An HttpKernelInterface instance
  155. */
  156. public function getKernel()
  157. {
  158. return $this->kernel;
  159. }
  160. /**
  161. * Gets the Surrogate instance.
  162. *
  163. * @return SurrogateInterface A Surrogate instance
  164. *
  165. * @throws \LogicException
  166. */
  167. public function getSurrogate()
  168. {
  169. return $this->surrogate;
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  175. {
  176. // FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism
  177. if (HttpKernelInterface::MASTER_REQUEST === $type) {
  178. $this->traces = [];
  179. // Keep a clone of the original request for surrogates so they can access it.
  180. // We must clone here to get a separate instance because the application will modify the request during
  181. // the application flow (we know it always does because we do ourselves by setting REMOTE_ADDR to 127.0.0.1
  182. // and adding the X-Forwarded-For header, see HttpCache::forward()).
  183. $this->request = clone $request;
  184. if (null !== $this->surrogate) {
  185. $this->surrogateCacheStrategy = $this->surrogate->createCacheStrategy();
  186. }
  187. }
  188. $this->traces[$this->getTraceKey($request)] = [];
  189. if (!$request->isMethodSafe()) {
  190. $response = $this->invalidate($request, $catch);
  191. } elseif ($request->headers->has('expect') || !$request->isMethodCacheable()) {
  192. $response = $this->pass($request, $catch);
  193. } elseif ($this->options['allow_reload'] && $request->isNoCache()) {
  194. /*
  195. If allow_reload is configured and the client requests "Cache-Control: no-cache",
  196. reload the cache by fetching a fresh response and caching it (if possible).
  197. */
  198. $this->record($request, 'reload');
  199. $response = $this->fetch($request, $catch);
  200. } else {
  201. $response = $this->lookup($request, $catch);
  202. }
  203. $this->restoreResponseBody($request, $response);
  204. if (HttpKernelInterface::MASTER_REQUEST === $type) {
  205. $this->addTraces($response);
  206. }
  207. if (null !== $this->surrogate) {
  208. if (HttpKernelInterface::MASTER_REQUEST === $type) {
  209. $this->surrogateCacheStrategy->update($response);
  210. } else {
  211. $this->surrogateCacheStrategy->add($response);
  212. }
  213. }
  214. $response->prepare($request);
  215. $response->isNotModified($request);
  216. return $response;
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function terminate(Request $request, Response $response)
  222. {
  223. if ($this->getKernel() instanceof TerminableInterface) {
  224. $this->getKernel()->terminate($request, $response);
  225. }
  226. }
  227. /**
  228. * Forwards the Request to the backend without storing the Response in the cache.
  229. *
  230. * @param bool $catch Whether to process exceptions
  231. *
  232. * @return Response A Response instance
  233. */
  234. protected function pass(Request $request, $catch = false)
  235. {
  236. $this->record($request, 'pass');
  237. return $this->forward($request, $catch);
  238. }
  239. /**
  240. * Invalidates non-safe methods (like POST, PUT, and DELETE).
  241. *
  242. * @param bool $catch Whether to process exceptions
  243. *
  244. * @return Response A Response instance
  245. *
  246. * @throws \Exception
  247. *
  248. * @see RFC2616 13.10
  249. */
  250. protected function invalidate(Request $request, $catch = false)
  251. {
  252. $response = $this->pass($request, $catch);
  253. // invalidate only when the response is successful
  254. if ($response->isSuccessful() || $response->isRedirect()) {
  255. try {
  256. $this->store->invalidate($request);
  257. // As per the RFC, invalidate Location and Content-Location URLs if present
  258. foreach (['Location', 'Content-Location'] as $header) {
  259. if ($uri = $response->headers->get($header)) {
  260. $subRequest = Request::create($uri, 'get', [], [], [], $request->server->all());
  261. $this->store->invalidate($subRequest);
  262. }
  263. }
  264. $this->record($request, 'invalidate');
  265. } catch (\Exception $e) {
  266. $this->record($request, 'invalidate-failed');
  267. if ($this->options['debug']) {
  268. throw $e;
  269. }
  270. }
  271. }
  272. return $response;
  273. }
  274. /**
  275. * Lookups a Response from the cache for the given Request.
  276. *
  277. * When a matching cache entry is found and is fresh, it uses it as the
  278. * response without forwarding any request to the backend. When a matching
  279. * cache entry is found but is stale, it attempts to "validate" the entry with
  280. * the backend using conditional GET. When no matching cache entry is found,
  281. * it triggers "miss" processing.
  282. *
  283. * @param bool $catch Whether to process exceptions
  284. *
  285. * @return Response A Response instance
  286. *
  287. * @throws \Exception
  288. */
  289. protected function lookup(Request $request, $catch = false)
  290. {
  291. try {
  292. $entry = $this->store->lookup($request);
  293. } catch (\Exception $e) {
  294. $this->record($request, 'lookup-failed');
  295. if ($this->options['debug']) {
  296. throw $e;
  297. }
  298. return $this->pass($request, $catch);
  299. }
  300. if (null === $entry) {
  301. $this->record($request, 'miss');
  302. return $this->fetch($request, $catch);
  303. }
  304. if (!$this->isFreshEnough($request, $entry)) {
  305. $this->record($request, 'stale');
  306. return $this->validate($request, $entry, $catch);
  307. }
  308. if ($entry->headers->hasCacheControlDirective('no-cache')) {
  309. return $this->validate($request, $entry, $catch);
  310. }
  311. $this->record($request, 'fresh');
  312. $entry->headers->set('Age', $entry->getAge());
  313. return $entry;
  314. }
  315. /**
  316. * Validates that a cache entry is fresh.
  317. *
  318. * The original request is used as a template for a conditional
  319. * GET request with the backend.
  320. *
  321. * @param bool $catch Whether to process exceptions
  322. *
  323. * @return Response A Response instance
  324. */
  325. protected function validate(Request $request, Response $entry, $catch = false)
  326. {
  327. $subRequest = clone $request;
  328. // send no head requests because we want content
  329. if ('HEAD' === $request->getMethod()) {
  330. $subRequest->setMethod('GET');
  331. }
  332. // add our cached last-modified validator
  333. if ($entry->headers->has('Last-Modified')) {
  334. $subRequest->headers->set('If-Modified-Since', $entry->headers->get('Last-Modified'));
  335. }
  336. // Add our cached etag validator to the environment.
  337. // We keep the etags from the client to handle the case when the client
  338. // has a different private valid entry which is not cached here.
  339. $cachedEtags = $entry->getEtag() ? [$entry->getEtag()] : [];
  340. $requestEtags = $request->getETags();
  341. if ($etags = array_unique(array_merge($cachedEtags, $requestEtags))) {
  342. $subRequest->headers->set('If-None-Match', implode(', ', $etags));
  343. }
  344. $response = $this->forward($subRequest, $catch, $entry);
  345. if (304 == $response->getStatusCode()) {
  346. $this->record($request, 'valid');
  347. // return the response and not the cache entry if the response is valid but not cached
  348. $etag = $response->getEtag();
  349. if ($etag && \in_array($etag, $requestEtags) && !\in_array($etag, $cachedEtags)) {
  350. return $response;
  351. }
  352. $entry = clone $entry;
  353. $entry->headers->remove('Date');
  354. foreach (['Date', 'Expires', 'Cache-Control', 'ETag', 'Last-Modified'] as $name) {
  355. if ($response->headers->has($name)) {
  356. $entry->headers->set($name, $response->headers->get($name));
  357. }
  358. }
  359. $response = $entry;
  360. } else {
  361. $this->record($request, 'invalid');
  362. }
  363. if ($response->isCacheable()) {
  364. $this->store($request, $response);
  365. }
  366. return $response;
  367. }
  368. /**
  369. * Unconditionally fetches a fresh response from the backend and
  370. * stores it in the cache if is cacheable.
  371. *
  372. * @param bool $catch Whether to process exceptions
  373. *
  374. * @return Response A Response instance
  375. */
  376. protected function fetch(Request $request, $catch = false)
  377. {
  378. $subRequest = clone $request;
  379. // send no head requests because we want content
  380. if ('HEAD' === $request->getMethod()) {
  381. $subRequest->setMethod('GET');
  382. }
  383. // avoid that the backend sends no content
  384. $subRequest->headers->remove('If-Modified-Since');
  385. $subRequest->headers->remove('If-None-Match');
  386. $response = $this->forward($subRequest, $catch);
  387. if ($response->isCacheable()) {
  388. $this->store($request, $response);
  389. }
  390. return $response;
  391. }
  392. /**
  393. * Forwards the Request to the backend and returns the Response.
  394. *
  395. * All backend requests (cache passes, fetches, cache validations)
  396. * run through this method.
  397. *
  398. * @param bool $catch Whether to catch exceptions or not
  399. * @param Response|null $entry A Response instance (the stale entry if present, null otherwise)
  400. *
  401. * @return Response A Response instance
  402. */
  403. protected function forward(Request $request, $catch = false, Response $entry = null)
  404. {
  405. if ($this->surrogate) {
  406. $this->surrogate->addSurrogateCapability($request);
  407. }
  408. // always a "master" request (as the real master request can be in cache)
  409. $response = SubRequestHandler::handle($this->kernel, $request, HttpKernelInterface::MASTER_REQUEST, $catch);
  410. /*
  411. * Support stale-if-error given on Responses or as a config option.
  412. * RFC 7234 summarizes in Section 4.2.4 (but also mentions with the individual
  413. * Cache-Control directives) that
  414. *
  415. * A cache MUST NOT generate a stale response if it is prohibited by an
  416. * explicit in-protocol directive (e.g., by a "no-store" or "no-cache"
  417. * cache directive, a "must-revalidate" cache-response-directive, or an
  418. * applicable "s-maxage" or "proxy-revalidate" cache-response-directive;
  419. * see Section 5.2.2).
  420. *
  421. * https://tools.ietf.org/html/rfc7234#section-4.2.4
  422. *
  423. * We deviate from this in one detail, namely that we *do* serve entries in the
  424. * stale-if-error case even if they have a `s-maxage` Cache-Control directive.
  425. */
  426. if (null !== $entry
  427. && \in_array($response->getStatusCode(), [500, 502, 503, 504])
  428. && !$entry->headers->hasCacheControlDirective('no-cache')
  429. && !$entry->mustRevalidate()
  430. ) {
  431. if (null === $age = $entry->headers->getCacheControlDirective('stale-if-error')) {
  432. $age = $this->options['stale_if_error'];
  433. }
  434. /*
  435. * stale-if-error gives the (extra) time that the Response may be used *after* it has become stale.
  436. * So we compare the time the $entry has been sitting in the cache already with the
  437. * time it was fresh plus the allowed grace period.
  438. */
  439. if ($entry->getAge() <= $entry->getMaxAge() + $age) {
  440. $this->record($request, 'stale-if-error');
  441. return $entry;
  442. }
  443. }
  444. /*
  445. RFC 7231 Sect. 7.1.1.2 says that a server that does not have a reasonably accurate
  446. clock MUST NOT send a "Date" header, although it MUST send one in most other cases
  447. except for 1xx or 5xx responses where it MAY do so.
  448. Anyway, a client that received a message without a "Date" header MUST add it.
  449. */
  450. if (!$response->headers->has('Date')) {
  451. $response->setDate(\DateTime::createFromFormat('U', time()));
  452. }
  453. $this->processResponseBody($request, $response);
  454. if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
  455. $response->setPrivate();
  456. } elseif ($this->options['default_ttl'] > 0 && null === $response->getTtl() && !$response->headers->getCacheControlDirective('must-revalidate')) {
  457. $response->setTtl($this->options['default_ttl']);
  458. }
  459. return $response;
  460. }
  461. /**
  462. * Checks whether the cache entry is "fresh enough" to satisfy the Request.
  463. *
  464. * @return bool true if the cache entry if fresh enough, false otherwise
  465. */
  466. protected function isFreshEnough(Request $request, Response $entry)
  467. {
  468. if (!$entry->isFresh()) {
  469. return $this->lock($request, $entry);
  470. }
  471. if ($this->options['allow_revalidate'] && null !== $maxAge = $request->headers->getCacheControlDirective('max-age')) {
  472. return $maxAge > 0 && $maxAge >= $entry->getAge();
  473. }
  474. return true;
  475. }
  476. /**
  477. * Locks a Request during the call to the backend.
  478. *
  479. * @return bool true if the cache entry can be returned even if it is staled, false otherwise
  480. */
  481. protected function lock(Request $request, Response $entry)
  482. {
  483. // try to acquire a lock to call the backend
  484. $lock = $this->store->lock($request);
  485. if (true === $lock) {
  486. // we have the lock, call the backend
  487. return false;
  488. }
  489. // there is already another process calling the backend
  490. // May we serve a stale response?
  491. if ($this->mayServeStaleWhileRevalidate($entry)) {
  492. $this->record($request, 'stale-while-revalidate');
  493. return true;
  494. }
  495. // wait for the lock to be released
  496. if ($this->waitForLock($request)) {
  497. // replace the current entry with the fresh one
  498. $new = $this->lookup($request);
  499. $entry->headers = $new->headers;
  500. $entry->setContent($new->getContent());
  501. $entry->setStatusCode($new->getStatusCode());
  502. $entry->setProtocolVersion($new->getProtocolVersion());
  503. foreach ($new->headers->getCookies() as $cookie) {
  504. $entry->headers->setCookie($cookie);
  505. }
  506. } else {
  507. // backend is slow as hell, send a 503 response (to avoid the dog pile effect)
  508. $entry->setStatusCode(503);
  509. $entry->setContent('503 Service Unavailable');
  510. $entry->headers->set('Retry-After', 10);
  511. }
  512. return true;
  513. }
  514. /**
  515. * Writes the Response to the cache.
  516. *
  517. * @throws \Exception
  518. */
  519. protected function store(Request $request, Response $response)
  520. {
  521. try {
  522. $this->store->write($request, $response);
  523. $this->record($request, 'store');
  524. $response->headers->set('Age', $response->getAge());
  525. } catch (\Exception $e) {
  526. $this->record($request, 'store-failed');
  527. if ($this->options['debug']) {
  528. throw $e;
  529. }
  530. }
  531. // now that the response is cached, release the lock
  532. $this->store->unlock($request);
  533. }
  534. /**
  535. * Restores the Response body.
  536. */
  537. private function restoreResponseBody(Request $request, Response $response)
  538. {
  539. if ($response->headers->has('X-Body-Eval')) {
  540. ob_start();
  541. if ($response->headers->has('X-Body-File')) {
  542. include $response->headers->get('X-Body-File');
  543. } else {
  544. eval('; ?>'.$response->getContent().'<?php ;');
  545. }
  546. $response->setContent(ob_get_clean());
  547. $response->headers->remove('X-Body-Eval');
  548. if (!$response->headers->has('Transfer-Encoding')) {
  549. $response->headers->set('Content-Length', \strlen($response->getContent()));
  550. }
  551. } elseif ($response->headers->has('X-Body-File')) {
  552. // Response does not include possibly dynamic content (ESI, SSI), so we need
  553. // not handle the content for HEAD requests
  554. if (!$request->isMethod('HEAD')) {
  555. $response->setContent(file_get_contents($response->headers->get('X-Body-File')));
  556. }
  557. } else {
  558. return;
  559. }
  560. $response->headers->remove('X-Body-File');
  561. }
  562. protected function processResponseBody(Request $request, Response $response)
  563. {
  564. if (null !== $this->surrogate && $this->surrogate->needsParsing($response)) {
  565. $this->surrogate->process($request, $response);
  566. }
  567. }
  568. /**
  569. * Checks if the Request includes authorization or other sensitive information
  570. * that should cause the Response to be considered private by default.
  571. */
  572. private function isPrivateRequest(Request $request): bool
  573. {
  574. foreach ($this->options['private_headers'] as $key) {
  575. $key = strtolower(str_replace('HTTP_', '', $key));
  576. if ('cookie' === $key) {
  577. if (\count($request->cookies->all())) {
  578. return true;
  579. }
  580. } elseif ($request->headers->has($key)) {
  581. return true;
  582. }
  583. }
  584. return false;
  585. }
  586. /**
  587. * Records that an event took place.
  588. */
  589. private function record(Request $request, string $event)
  590. {
  591. $this->traces[$this->getTraceKey($request)][] = $event;
  592. }
  593. /**
  594. * Calculates the key we use in the "trace" array for a given request.
  595. */
  596. private function getTraceKey(Request $request): string
  597. {
  598. $path = $request->getPathInfo();
  599. if ($qs = $request->getQueryString()) {
  600. $path .= '?'.$qs;
  601. }
  602. return $request->getMethod().' '.$path;
  603. }
  604. /**
  605. * Checks whether the given (cached) response may be served as "stale" when a revalidation
  606. * is currently in progress.
  607. */
  608. private function mayServeStaleWhileRevalidate(Response $entry): bool
  609. {
  610. $timeout = $entry->headers->getCacheControlDirective('stale-while-revalidate');
  611. if (null === $timeout) {
  612. $timeout = $this->options['stale_while_revalidate'];
  613. }
  614. return abs($entry->getTtl()) < $timeout;
  615. }
  616. /**
  617. * Waits for the store to release a locked entry.
  618. */
  619. private function waitForLock(Request $request): bool
  620. {
  621. $wait = 0;
  622. while ($this->store->isLocked($request) && $wait < 100) {
  623. usleep(50000);
  624. ++$wait;
  625. }
  626. return $wait < 100;
  627. }
  628. }