Example #1
0
 /**
  * Send curl purge request
  * to invalidate cache by tags pattern
  *
  * @param string $tagsPattern
  * @return bool Return true if successful; otherwise return false
  */
 public function sendPurgeRequest($tagsPattern)
 {
     $socketAdapter = $this->socketAdapterFactory->create();
     $servers = $this->cacheServer->getUris();
     $headers = [self::HEADER_X_MAGENTO_TAGS_PATTERN => $tagsPattern];
     $socketAdapter->setOptions(['timeout' => 10]);
     foreach ($servers as $server) {
         try {
             $socketAdapter->connect($server->getHost(), $server->getPort());
             $socketAdapter->write('PURGE', $server, '1.1', $headers);
             $socketAdapter->close();
         } catch (\Exception $e) {
             $this->logger->critical($e->getMessage(), compact('server', 'tagsPattern'));
             return false;
         }
     }
     $this->logger->execute(compact('servers', 'tagsPattern'));
     return true;
 }
 /**
  * @param int $getHttpHostCallCtr
  * @param string $httpHost
  * @param int $getUrlCallCtr
  * @param string $url
  * @param string[] $hostConfig
  * @dataProvider getUrisDataProvider
  */
 public function testGetUris($getHttpHostCallCtr, $httpHost, $getUrlCallCtr, $url, $hostConfig = null)
 {
     $this->configMock->expects($this->once())->method('get')->willReturn($hostConfig);
     $this->requestMock->expects($this->exactly($getHttpHostCallCtr))->method('getHttpHost')->willReturn($httpHost);
     $this->urlBuilderMock->expects($this->exactly($getUrlCallCtr))->method('getUrl')->with('*', ['_nosid' => true])->willReturn($url);
     $uris = [];
     if (null === $hostConfig) {
         if (!empty($httpHost)) {
             $uris[] = UriFactory::factory('')->setHost($httpHost)->setPort(\Magento\PageCache\Model\Cache\Server::DEFAULT_PORT)->setScheme('http');
         }
         if (!empty($url)) {
             $uris[] = UriFactory::factory($url);
         }
     } else {
         foreach ($hostConfig as $host) {
             $port = isset($host['port']) ? $host['port'] : \Magento\PageCache\Model\Cache\Server::DEFAULT_PORT;
             $uris[] = UriFactory::factory('')->setHost($host['host'])->setPort($port)->setScheme('http');
         }
     }
     $this->assertEquals($uris, $this->model->getUris());
 }