/** * @param CacheStrategyInterface $cache * @param RequestInterface $request * @param ResponseInterface $response * @param bool $update cache * @return ResponseInterface */ protected static function addToCache(CacheStrategyInterface $cache, RequestInterface $request, ResponseInterface $response, $update = false) { // If the body is not seekable, we have to replace it by a seekable one if (!$response->getBody()->isSeekable()) { $response = $response->withBody(\GuzzleHttp\Psr7\stream_for($response->getBody()->getContents())); } if ($update) { $cache->update($request, $response); } else { $cache->cache($request, $response); } return $response; }
/** * @param RequestInterface $request * @param CacheStrategyInterface $cacheStorage * @param CacheEntry $cacheEntry * * @return bool if added */ protected function addReValidationRequest(RequestInterface $request, CacheStrategyInterface &$cacheStorage, CacheEntry $cacheEntry) { // Add the promise for revalidate if ($this->client !== null) { /** @var RequestInterface $request */ $request = $request->withHeader(self::HEADER_RE_VALIDATION, '1'); $this->waitingRevalidate[] = $this->client->sendAsync($request)->then(function (ResponseInterface $response) use($request, &$cacheStorage, $cacheEntry) { if ($response->getStatusCode() == 304) { // Not modified => cache entry is re-validate /** @var ResponseInterface $response */ $response = $response->withStatus($cacheEntry->getResponse()->getStatusCode()); $response = $response->withBody($cacheEntry->getResponse()->getBody()); } $cacheStorage->cache($request, $response); }); return true; } return false; }