示例#1
0
    public function testInvalidateTags()
    {
        $cacheInvalidator = new CacheInvalidator($this->getProxyClient());

        $this->assertMiss($this->getResponse('/tags.php'));
        $this->assertHit($this->getResponse('/tags.php'));

        $cacheInvalidator->invalidateTags(array('tag1'))->flush();

        $this->assertMiss($this->getResponse('/tags.php'));
    }
示例#2
0
 /**
  * Invalidate cache entries that contain any of the specified tags in their
  * tag header.
  *
  * @param array $tags Cache tags
  *
  * @return $this
  */
 public function invalidateTags(array $tags)
 {
     $tagExpression = sprintf('(%s)(,.+)?$', implode('|', array_map('preg_quote', $this->escapeTags($tags))));
     $headers = array($this->tagsHeader => $tagExpression);
     $this->invalidator->invalidate($headers);
     return $this;
 }
 /**
  * @return void
  */
 protected function execute()
 {
     try {
         $this->cacheInvalidator->flush();
     } catch (ExceptionCollection $exceptions) {
         foreach ($exceptions as $exception) {
             if ($exception instanceof ProxyResponseException) {
                 $this->systemLogger->log(sprintf('Error calling Varnish with BAN request (cannot connect to the caching proxy). Error %s', $exception->getMessage()), LOG_ERR);
             } elseif ($exception instanceof ProxyUnreachableException) {
                 $this->systemLogger->log(sprintf('Error calling Varnish with BAN request (caching proxy returned an error response). Error %s', $exception->getMessage()), LOG_ERR);
             } else {
                 $this->systemLogger->log(sprintf('Error calling Varnish with BAN request. Error %s', $exception->getMessage()), LOG_ERR);
             }
         }
     }
 }
 /**
  * Invalidate cache entries that contain any of the specified tags in their
  * tag header.
  *
  * @param array $tags Cache tags
  *
  * @return $this
  */
 public function invalidateTags(array $tags)
 {
     $escapedTags = array_map('preg_quote', $this->escapeTags($tags));
     if (mb_strlen(implode('|', $escapedTags)) >= $this->headerLength) {
         /*
          * estimate the amount of tags to invalidate by dividing the max
          * header length by the largest tag (minus 1 for the implode character)
          */
         $tagsize = max(array_map('mb_strlen', $escapedTags));
         $elems = floor($this->headerLength / ($tagsize - 1)) ?: 1;
     } else {
         $elems = count($escapedTags);
     }
     foreach (array_chunk($escapedTags, $elems) as $tagchunk) {
         $tagExpression = sprintf('(%s)(,.+)?$', implode('|', $tagchunk));
         $headers = array($this->tagsHeader => $tagExpression);
         $this->invalidator->invalidate($headers);
     }
     return $this;
 }
 /**
  * Constructor
  *
  * @param ProxyClientInterface  $cache        HTTP cache proxy client
  * @param UrlGeneratorInterface $urlGenerator Symfony route generator
  */
 public function __construct(ProxyClientInterface $cache, UrlGeneratorInterface $urlGenerator)
 {
     parent::__construct($cache);
     $this->urlGenerator = $urlGenerator;
 }
示例#6
0
    public function testEventDispatcher()
    {
        $proxyClient = new Varnish(array('localhost'));

        $cacheInvalidator = new CacheInvalidator($proxyClient);
        $eventDispatcher = new EventDispatcher();
        $cacheInvalidator->setEventDispatcher($eventDispatcher);
        $this->assertSame($eventDispatcher, $cacheInvalidator->getEventDispatcher());
    }