EsiResponseCacheStrategy.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This code is partially based on the Rack-Cache library by Ryan Tomayko,
  8. * which is released under the MIT license.
  9. * (based on commit 02d2b48d75bcb63cf1c0c7149c077ad256542801)
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. namespace Symfony\Component\HttpKernel\HttpCache;
  15. use Symfony\Component\HttpFoundation\Response;
  16. /**
  17. * EsiResponseCacheStrategy knows how to compute the Response cache HTTP header
  18. * based on the different ESI response cache headers.
  19. *
  20. * This implementation changes the master response TTL to the smallest TTL received
  21. * or force validation if one of the ESI has validation cache strategy.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class EsiResponseCacheStrategy implements EsiResponseCacheStrategyInterface
  26. {
  27. private $cacheable = true;
  28. private $embeddedResponses = 0;
  29. private $ttls = array();
  30. private $maxAges = array();
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function add(Response $response)
  35. {
  36. if ($response->isValidateable()) {
  37. $this->cacheable = false;
  38. } else {
  39. $this->ttls[] = $response->getTtl();
  40. $this->maxAges[] = $response->getMaxAge();
  41. }
  42. $this->embeddedResponses++;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function update(Response $response)
  48. {
  49. // if we have no embedded Response, do nothing
  50. if (0 === $this->embeddedResponses) {
  51. return;
  52. }
  53. // Remove validation related headers in order to avoid browsers using
  54. // their own cache, because some of the response content comes from
  55. // at least one embedded response (which likely has a different caching strategy).
  56. if ($response->isValidateable()) {
  57. $response->setEtag(null);
  58. $response->setLastModified(null);
  59. $this->cacheable = false;
  60. }
  61. if (!$this->cacheable) {
  62. $response->headers->set('Cache-Control', 'no-cache, must-revalidate');
  63. return;
  64. }
  65. $this->ttls[] = $response->getTtl();
  66. $this->maxAges[] = $response->getMaxAge();
  67. if (null !== $maxAge = min($this->maxAges)) {
  68. $response->setSharedMaxAge($maxAge);
  69. $response->headers->set('Age', $maxAge - min($this->ttls));
  70. }
  71. $response->setMaxAge(0);
  72. }
  73. }