Skip to content
This repository has been archived by the owner on Dec 5, 2017. It is now read-only.

Commit

Permalink
Merge pull request #40 from liip/varnish_invalidate_command
Browse files Browse the repository at this point in the history
Varnish invalidate command
  • Loading branch information
dbu committed Dec 19, 2013
2 parents e84ae12 + db929a8 commit 1157f18
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 5 deletions.
79 changes: 79 additions & 0 deletions Command/InvalidateVarnishCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Liip\CacheControlBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;


/**
* Importing zips to cooperatives mapping
*/
class InvalidateVarnishCommand extends ContainerAwareCommand
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* Configures the current command.
*
* @return void
*/
protected function configure()
{
$this
->setName('liip:cache-control:varnish:invalidate')
->setDescription('Clear cached entries from Varnish servers')
->addArgument(
'path',
InputArgument::OPTIONAL,
'What URLs do you want to invalidate? (default: .)'
);
}

/**
* Executes the current command.
*
* @param InputInterface $input Input
* @param OutputInterface $output Output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();

$this->logger = $container->get('logger');
$helper = $this->getContainer()->get('liip_cache_control.varnish');

$path = $input->getArgument('path');
if (!$path) {
$path = ".";
}
$this->getLogger()->notice('Starting clearing varnish with path: "' . $path .'"');

$helper->invalidatePath($path);

$this->getLogger()->notice('Done clearing varnish');
}

/**
* Returns the logger
*
* @return LoggerInterface
*/
protected function getLogger()
{
if (null == $this->logger) {
$this->logger = new NullLogger();
}

return $this->logger;
}
}
5 changes: 5 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ private function addVarnishSection(ArrayNodeDefinition $rootNode)
->scalarNode('domain')->defaultNull()->info('depreciated, use host instead')->end()
->scalarNode('host')->defaultNull()->info('URL host name')->end()
->scalarNode('port')->defaultNull()->end()
->arrayNode('headers')
->treatNullLike(array())
->prototype('scalar')->end()
->info('Extra HTTP headers sent to varnish')
->end()
->enumNode('purge_instruction')
->values(array('purge', 'ban'))
->defaultValue('purge')
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/LiipCacheControlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter($this->getAlias().'.varnish.host', $host);
$container->setParameter($this->getAlias().'.varnish.port', $config['varnish']['port']);
$container->setParameter($this->getAlias().'.varnish.purge_instruction', $config['varnish']['purge_instruction']);
$container->setParameter($this->getAlias().'.varnish.headers', $config['varnish']['headers']);
}

if ($config['authorization_listener']) {
Expand Down
20 changes: 15 additions & 5 deletions Helper/Varnish.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Varnish
private $host;
private $port;
private $purgeInstruction;
private $headers;

private $lastRequestError;
private $lastRequestInfo;
Expand All @@ -68,17 +69,24 @@ class Varnish
* port for all instances)
* @param string $purgeInstruction the purge instruction (purge in Varnish
* 2, ban possible since Varnish 3)
* @param array $headers Extra HTTP headers sent to the varnish servers on
* each request.
*/
public function __construct($host, array $ips, $port, $purgeInstruction = self::PURGE_INSTRUCTION_PURGE)
public function __construct($host, array $ips, $port, $purgeInstruction = self::PURGE_INSTRUCTION_PURGE, array $headers = array())
{
$url = parse_url($host);
$this->host = $url['host'];
if (isset($url['port'])) {
$this->host .= ':' . $url['port'];
if (substr($host,0,7) == "http://") {
$url = parse_url($host);
$this->host = $url['host'];
if (isset($url['port'])) {
$this->host .= ':' . $url['port'];
}
} else {
$this->host = $host;
}
$this->ips = $ips;
$this->port = $port;
$this->purgeInstruction = $purgeInstruction;
$this->headers = $headers;
}

/**
Expand Down Expand Up @@ -200,6 +208,8 @@ protected function sendRequestToAllVarnishes($path, array $headers = array(), ar
$options[CURLOPT_HTTPHEADER] = $headers;
}

$options[CURLOPT_HTTPHEADER] = array_merge($this->headers, $options[CURLOPT_HTTPHEADER]);

foreach ($options as $option => $value) {
curl_setopt($curlHandler, (int) $option, $value);
}
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,17 @@ liip_cache_control:
host: http://www.liip.ch
ips: 10.0.0.10, 10.0.0.11
port: 80
headers: ["Authorization: Basic Zm9vOmJhcg==", "X-Another-Header: here"]
```

* **host**: This must match the web host clients are using when connecting to varnish.
You will not notice if this is mistyped, but cache invalidation will never happen.
You can also add a regexp here like ".*" to clear all host entries. The regexp will be
surrounded by "^(" and ")$" ending in "^(.*)$" in this example.
* **ips**: List of IP adresses of your varnish servers. Comma separated.
* **port**: The port varnish is listening on for incoming web connections.
* **headers**: (optional) If you want to send special headers with each request sent to varnish,
you can add them here (as array)

To use the varnish cache helper you must inject the
``liip_cache_control.varnish`` service or fetch it from the service container:
Expand Down Expand Up @@ -395,6 +400,30 @@ $varnish = $this->container->get('liip_cache_control.varnish');
$varnish->refreshPath('/some/path');
```

Banning from the console
------------------------

You can also ban URLs from the console

``` shell
app/console liip:cache-control:varnish:invalidate
```

will ban (invalidate) all entries in your configured varnish servers (matching
varnish.host)

``` shell
app/console liip:cache-control:varnish:invalidate /posts.*
```

will ban (invalidate) all entries in your configured varnish servers, where the
URL starts with "/posts". Any regular expression understood by varnish can be
used here.

It uses the Varnish Helper class, therefore if you defined more than one varnish
server in the config file (in varnish.ips), the entries will be deleted in all
servers.

Cache authorization listener
============================

Expand Down Expand Up @@ -512,3 +541,5 @@ liip_cache_control:
flash_message_listener:
enabled: false
```
1 change: 1 addition & 0 deletions Resources/config/varnish_helper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<argument>%liip_cache_control.varnish.ips%</argument>
<argument>%liip_cache_control.varnish.port%</argument>
<argument>%liip_cache_control.varnish.purge_instruction%</argument>
<argument>%liip_cache_control.varnish.headers%</argument>
</service>

</services>
Expand Down

0 comments on commit 1157f18

Please sign in to comment.