Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy::getCacheObject PHP Méthode

getCacheObject() protected méthode

protected getCacheObject ( Psr\Http\Message\RequestInterface $request, Psr\Http\Message\ResponseInterface $response ) : CacheEntry | null
$request Psr\Http\Message\RequestInterface
$response Psr\Http\Message\ResponseInterface
Résultat Kevinrob\GuzzleCache\CacheEntry | null entry to save, null if can't cache it
    protected function getCacheObject(RequestInterface $request, ResponseInterface $response)
    {
        if (!isset($this->statusAccepted[$response->getStatusCode()])) {
            // Don't cache it
            return;
        }
        $cacheControl = new KeyValueHttpHeader($response->getHeader('Cache-Control'));
        $varyHeader = new KeyValueHttpHeader($response->getHeader('Vary'));
        if ($varyHeader->has('*')) {
            // This will never match with a request
            return;
        }
        if ($cacheControl->has('no-store')) {
            // No store allowed (maybe some sensitives data...)
            return;
        }
        if ($cacheControl->has('no-cache')) {
            // Stale response see RFC7234 section 5.2.1.4
            $entry = new CacheEntry($request, $response, new \DateTime('-1 seconds'));
            return $entry->hasValidationInformation() ? $entry : null;
        }
        foreach ($this->ageKey as $key) {
            if ($cacheControl->has($key)) {
                return new CacheEntry($request, $response, new \DateTime('+' . (int) $cacheControl->get($key) . 'seconds'));
            }
        }
        if ($response->hasHeader('Expires')) {
            $expireAt = \DateTime::createFromFormat(\DateTime::RFC1123, $response->getHeaderLine('Expires'));
            if ($expireAt !== false) {
                return new CacheEntry($request, $response, $expireAt);
            }
        }
        return new CacheEntry($request, $response, new \DateTime('-1 seconds'));
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 protected function getCacheObject(RequestInterface $request, ResponseInterface $response)
 {
     $cacheControl = new KeyValueHttpHeader($response->getHeader('Cache-Control'));
     if ($cacheControl->has('private')) {
         return;
     }
     return parent::getCacheObject($request, $response);
 }