Drupal investigation

ResponseCacheStrategy.php 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * ResponseCacheStrategy knows how to compute the Response cache HTTP header
  18. * based on the different 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 surrogates has validation cache strategy.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. */
  25. class ResponseCacheStrategy implements ResponseCacheStrategyInterface
  26. {
  27. private $cacheable = true;
  28. private $embeddedResponses = 0;
  29. private $ttls = array();
  30. private $maxAges = array();
  31. private $isNotCacheableResponseEmbedded = false;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function add(Response $response)
  36. {
  37. if ($response->isValidateable()) {
  38. $this->cacheable = false;
  39. } else {
  40. $maxAge = $response->getMaxAge();
  41. $this->ttls[] = $response->getTtl();
  42. $this->maxAges[] = $maxAge;
  43. if (null === $maxAge) {
  44. $this->isNotCacheableResponseEmbedded = true;
  45. }
  46. }
  47. ++$this->embeddedResponses;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function update(Response $response)
  53. {
  54. // if we have no embedded Response, do nothing
  55. if (0 === $this->embeddedResponses) {
  56. return;
  57. }
  58. // Remove validation related headers in order to avoid browsers using
  59. // their own cache, because some of the response content comes from
  60. // at least one embedded response (which likely has a different caching strategy).
  61. if ($response->isValidateable()) {
  62. $response->setEtag(null);
  63. $response->setLastModified(null);
  64. $this->cacheable = false;
  65. }
  66. if (!$this->cacheable) {
  67. $response->headers->set('Cache-Control', 'no-cache, must-revalidate');
  68. return;
  69. }
  70. $this->ttls[] = $response->getTtl();
  71. $this->maxAges[] = $response->getMaxAge();
  72. if ($this->isNotCacheableResponseEmbedded) {
  73. $response->headers->removeCacheControlDirective('s-maxage');
  74. } elseif (null !== $maxAge = min($this->maxAges)) {
  75. $response->setSharedMaxAge($maxAge);
  76. $response->headers->set('Age', $maxAge - min($this->ttls));
  77. }
  78. $response->setMaxAge(0);
  79. }
  80. }