/** * Purge a request from the cache storage * * @param RequestInterface $request Request to purge */ public function purge(RequestInterface $request) { // If the request has a cache.purge_methods param, then use that, otherwise use the default known methods $methods = $request->getParams()->get('cache.purge_methods') ?: array('GET', 'HEAD', 'POST', 'PUT', 'DELETE'); foreach ($methods as $method) { // Clone the request with each method and clear from the cache $cloned = RequestFactory::getInstance()->cloneRequestWithMethod($request, $method); $key = $this->keyProvider->getCacheKey($cloned); $this->storage->delete($key); } }
/** * If possible, store a response in cache after sending * * @param Event $event */ public function onRequestSent(Event $event) { $request = $event['request']; $response = $event['response']; if (isset($this->cached[$request])) { $cacheKey = $this->cached[$request]; unset($this->cached[$request]); if ($this->canCache->canCacheResponse($response)) { $this->storage->cache($cacheKey, $response, $request->getParams()->get('cache.override_ttl') ?: $response->getMaxAge()); } } }
/** * If possible, set a cache response on a cURL exception * * @param Event $event */ public function onRequestException(Event $event) { if (!$event['exception'] instanceof CurlException) { return; } $request = $event['request']; if (!$this->canCache->canCacheRequest($request)) { return; } $cacheKey = $this->keyProvider->getCacheKey($request); if ($cachedData = $this->storage->fetch($cacheKey)) { $response = new Response($cachedData[0], $cachedData[1], $cachedData[2]); $response->setHeader('Age', time() - strtotime($response->getDate() ?: 'now')); if (!$this->canResponseSatisfyFailedRequest($request, $response)) { return; } $request->getParams()->set('cache.hit', 'error'); $request->setResponse($response); $event->stopPropagation(); } }
/** * Purge all cache entries for a given URL * * @param string $url URL to purge */ public function purge($url) { // BC compatibility with previous version that accepted a Request object $url = $url instanceof RequestInterface ? $url->getUrl() : $url; $this->storage->purge($url); }