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

Commit 1157f18

Browse files
committed
Merge pull request #40 from liip/varnish_invalidate_command
Varnish invalidate command
2 parents e84ae12 + db929a8 commit 1157f18

6 files changed

Lines changed: 132 additions & 5 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Liip\CacheControlBundle\Command;
4+
5+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
use Psr\Log\LoggerInterface;
7+
use Psr\Log\NullLogger;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\Input\InputArgument;
11+
12+
13+
/**
14+
* Importing zips to cooperatives mapping
15+
*/
16+
class InvalidateVarnishCommand extends ContainerAwareCommand
17+
{
18+
/**
19+
* @var LoggerInterface
20+
*/
21+
private $logger;
22+
23+
/**
24+
* Configures the current command.
25+
*
26+
* @return void
27+
*/
28+
protected function configure()
29+
{
30+
$this
31+
->setName('liip:cache-control:varnish:invalidate')
32+
->setDescription('Clear cached entries from Varnish servers')
33+
->addArgument(
34+
'path',
35+
InputArgument::OPTIONAL,
36+
'What URLs do you want to invalidate? (default: .)'
37+
);
38+
}
39+
40+
/**
41+
* Executes the current command.
42+
*
43+
* @param InputInterface $input Input
44+
* @param OutputInterface $output Output
45+
*
46+
* @return void
47+
*/
48+
protected function execute(InputInterface $input, OutputInterface $output)
49+
{
50+
$container = $this->getContainer();
51+
52+
$this->logger = $container->get('logger');
53+
$helper = $this->getContainer()->get('liip_cache_control.varnish');
54+
55+
$path = $input->getArgument('path');
56+
if (!$path) {
57+
$path = ".";
58+
}
59+
$this->getLogger()->notice('Starting clearing varnish with path: "' . $path .'"');
60+
61+
$helper->invalidatePath($path);
62+
63+
$this->getLogger()->notice('Done clearing varnish');
64+
}
65+
66+
/**
67+
* Returns the logger
68+
*
69+
* @return LoggerInterface
70+
*/
71+
protected function getLogger()
72+
{
73+
if (null == $this->logger) {
74+
$this->logger = new NullLogger();
75+
}
76+
77+
return $this->logger;
78+
}
79+
}

DependencyInjection/Configuration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ private function addVarnishSection(ArrayNodeDefinition $rootNode)
102102
->scalarNode('domain')->defaultNull()->info('depreciated, use host instead')->end()
103103
->scalarNode('host')->defaultNull()->info('URL host name')->end()
104104
->scalarNode('port')->defaultNull()->end()
105+
->arrayNode('headers')
106+
->treatNullLike(array())
107+
->prototype('scalar')->end()
108+
->info('Extra HTTP headers sent to varnish')
109+
->end()
105110
->enumNode('purge_instruction')
106111
->values(array('purge', 'ban'))
107112
->defaultValue('purge')

DependencyInjection/LiipCacheControlExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function load(array $configs, ContainerBuilder $container)
7676
$container->setParameter($this->getAlias().'.varnish.host', $host);
7777
$container->setParameter($this->getAlias().'.varnish.port', $config['varnish']['port']);
7878
$container->setParameter($this->getAlias().'.varnish.purge_instruction', $config['varnish']['purge_instruction']);
79+
$container->setParameter($this->getAlias().'.varnish.headers', $config['varnish']['headers']);
7980
}
8081

8182
if ($config['authorization_listener']) {

Helper/Varnish.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Varnish
5454
private $host;
5555
private $port;
5656
private $purgeInstruction;
57+
private $headers;
5758

5859
private $lastRequestError;
5960
private $lastRequestInfo;
@@ -68,17 +69,24 @@ class Varnish
6869
* port for all instances)
6970
* @param string $purgeInstruction the purge instruction (purge in Varnish
7071
* 2, ban possible since Varnish 3)
72+
* @param array $headers Extra HTTP headers sent to the varnish servers on
73+
* each request.
7174
*/
72-
public function __construct($host, array $ips, $port, $purgeInstruction = self::PURGE_INSTRUCTION_PURGE)
75+
public function __construct($host, array $ips, $port, $purgeInstruction = self::PURGE_INSTRUCTION_PURGE, array $headers = array())
7376
{
74-
$url = parse_url($host);
75-
$this->host = $url['host'];
76-
if (isset($url['port'])) {
77-
$this->host .= ':' . $url['port'];
77+
if (substr($host,0,7) == "http://") {
78+
$url = parse_url($host);
79+
$this->host = $url['host'];
80+
if (isset($url['port'])) {
81+
$this->host .= ':' . $url['port'];
82+
}
83+
} else {
84+
$this->host = $host;
7885
}
7986
$this->ips = $ips;
8087
$this->port = $port;
8188
$this->purgeInstruction = $purgeInstruction;
89+
$this->headers = $headers;
8290
}
8391

8492
/**
@@ -200,6 +208,8 @@ protected function sendRequestToAllVarnishes($path, array $headers = array(), ar
200208
$options[CURLOPT_HTTPHEADER] = $headers;
201209
}
202210

211+
$options[CURLOPT_HTTPHEADER] = array_merge($this->headers, $options[CURLOPT_HTTPHEADER]);
212+
203213
foreach ($options as $option => $value) {
204214
curl_setopt($curlHandler, (int) $option, $value);
205215
}

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,17 @@ liip_cache_control:
189189
host: http://www.liip.ch
190190
ips: 10.0.0.10, 10.0.0.11
191191
port: 80
192+
headers: ["Authorization: Basic Zm9vOmJhcg==", "X-Another-Header: here"]
192193
```
193194

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

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

403+
Banning from the console
404+
------------------------
405+
406+
You can also ban URLs from the console
407+
408+
``` shell
409+
app/console liip:cache-control:varnish:invalidate
410+
```
411+
412+
will ban (invalidate) all entries in your configured varnish servers (matching
413+
varnish.host)
414+
415+
``` shell
416+
app/console liip:cache-control:varnish:invalidate /posts.*
417+
```
418+
419+
will ban (invalidate) all entries in your configured varnish servers, where the
420+
URL starts with "/posts". Any regular expression understood by varnish can be
421+
used here.
422+
423+
It uses the Varnish Helper class, therefore if you defined more than one varnish
424+
server in the config file (in varnish.ips), the entries will be deleted in all
425+
servers.
426+
398427
Cache authorization listener
399428
============================
400429

@@ -512,3 +541,5 @@ liip_cache_control:
512541
flash_message_listener:
513542
enabled: false
514543
```
544+
545+

Resources/config/varnish_helper.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<argument>%liip_cache_control.varnish.ips%</argument>
1212
<argument>%liip_cache_control.varnish.port%</argument>
1313
<argument>%liip_cache_control.varnish.purge_instruction%</argument>
14+
<argument>%liip_cache_control.varnish.headers%</argument>
1415
</service>
1516

1617
</services>

0 commit comments

Comments
 (0)