Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  namespace Kevinrob\GuzzleCache\Strategy;
   4  
   5  use Kevinrob\GuzzleCache\KeyValueHttpHeader;
   6  use Kevinrob\GuzzleCache\Storage\CacheStorageInterface;
   7  use Psr\Http\Message\RequestInterface;
   8  use Psr\Http\Message\ResponseInterface;
   9  
  10  /**
  11   * This strategy represents a "public" or "shared" HTTP client.
  12   * You can share the storage between applications.
  13   *
  14   * For example, a response with cache-control header "private, max-age=60"
  15   * will be NOT cached by this strategy.
  16   *
  17   * The rules applied are from RFC 7234.
  18   *
  19   * @see https://tools.ietf.org/html/rfc7234
  20   */
  21  class PublicCacheStrategy extends PrivateCacheStrategy
  22  {
  23      public function __construct(CacheStorageInterface $cache = null)
  24      {
  25          parent::__construct($cache);
  26  
  27          array_unshift($this->ageKey, 's-maxage');
  28      }
  29  
  30      /**
  31       * {@inheritdoc}
  32       */
  33      protected function getCacheObject(RequestInterface $request, ResponseInterface $response)
  34      {
  35          $cacheControl = new KeyValueHttpHeader($response->getHeader('Cache-Control'));
  36          if ($cacheControl->has('private')) {
  37              return;
  38          }
  39  
  40          return parent::getCacheObject($request, $response);
  41      }
  42  }