Skip to content

Commit 40b24bd

Browse files
committed
adding client for the symfony built-in reverse proxy HttpCache
1 parent b56f545 commit 40b24bd

File tree

8 files changed

+525
-238
lines changed

8 files changed

+525
-238
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
1.3.3
5+
-----
6+
7+
* **2015-05-08** Added a client for the Symfony built-in HttpCache
8+
49
1.3.0
510
-----
611

doc/proxy-clients.rst

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Caching Proxy Clients
22
=====================
33

4-
This library ships with clients for the Varnish and NGINX caching proxy. You
4+
This library ships with clients for the Varnish, NGINX and Symfony built-in caching proxies. You
55
can use the clients either wrapped by the :doc:`cache invalidator <cache-invalidator>`
66
(recommended), or directly for low-level access to invalidation functionality.
77

@@ -35,16 +35,16 @@ include that port in the base URL::
3535

3636
.. note::
3737

38-
To use the client, you need to :doc:`configure Varnish <varnish-configuration>` accordingly.
38+
To make invalidation work, you need to :doc:`configure Varnish <varnish-configuration>` accordingly.
3939

4040
NGINX Client
4141
~~~~~~~~~~~~
4242

4343
At minimum, supply an array containing IPs or hostnames of the NGINX servers
4444
that you want to send invalidation requests to. Make sure to include the port
45-
NGINX runs on if it is not port 80::
45+
NGINX runs on if it is not the default::
4646

47-
use FOS\HttpCache\Invalidation\Nginx;
47+
use FOS\HttpCache\ProxyClient\Nginx;
4848

4949
$servers = array('10.0.0.1', '10.0.0.2:8088'); // Port 80 assumed for 10.0.0.1
5050
$nginx = new Nginx($servers);
@@ -64,6 +64,29 @@ supply that location to the class as the third parameter::
6464

6565
To use the client, you need to :doc:`configure NGINX <nginx-configuration>` accordingly.
6666

67+
Symfony Client
68+
~~~~~~~~~~~~~~
69+
70+
At minimum, supply an array containing IPs or hostnames of your web servers
71+
running Symfony. Provide the direct access to the web server without any other
72+
proxies that might block invalidation requests. Make sure to include the port
73+
the web server runs on if it is not the default::
74+
75+
use FOS\HttpCache\ProxyClient\Symfony;
76+
77+
$servers = array('10.0.0.1', '10.0.0.2:8088'); // Port 80 assumed for 10.0.0.1
78+
$client = new Symfony($servers);
79+
80+
This is sufficient for invalidating absolute URLs. If you also wish to
81+
invalidate relative paths, supply the hostname (or base URL) where your website
82+
is available as the second parameter::
83+
84+
$client = new Symfony($servers, 'my-cool-app.com');
85+
86+
.. note::
87+
88+
To make invalidation work, you need to :doc:`use the EventDispatchingHttpCache <symfony-cache-configuration>`.
89+
6790
Using the Clients
6891
-----------------
6992

@@ -200,4 +223,7 @@ send a basic authentication header, you can inject a custom Guzzle client::
200223
$servers = array('10.0.0.1');
201224
$varnish = new Varnish($servers, '/baseUrl', $client);
202225

226+
The Symfony client accepts a guzzle client as the 3rd parameter as well, NGINX
227+
accepts it as 4th parameter.
228+
203229
.. _Guzzle client: http://guzzle3.readthedocs.org/

doc/symfony-cache-configuration.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ not the recommended way. You would need to adjust every place you instantiate
4242
the cache. Instead, overwrite the constructor of AppCache and register the
4343
subscribers there. A simple cache will look like this::
4444

45-
require_once __DIR__.'/AppKernel.php';
46-
4745
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
4846
use FOS\HttpCache\SymfonyCache\UserContextSubscriber;
4947

@@ -57,6 +55,8 @@ subscribers there. A simple cache will look like this::
5755
parent::__construct($kernel, $cacheDir);
5856

5957
$this->addSubscriber(new UserContextSubscriber());
58+
$this->addSubscriber(new PurgeSubscriber());
59+
$this->addSubscriber(new RefreshSubscriber());
6060
}
6161
}
6262

@@ -168,7 +168,7 @@ options through the constructor:
168168

169169
With Apache, you can do this for example in a ``.htaccess`` file::
170170

171-
RewriteEngine On
171+
RewriteEngine On
172172
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
173173

174174
Cleaning the Cookie Header

src/ProxyClient/Symfony.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\ProxyClient;
13+
14+
use FOS\HttpCache\Exception\InvalidArgumentException;
15+
use FOS\HttpCache\Exception\MissingHostException;
16+
use FOS\HttpCache\ProxyClient\Invalidation\BanInterface;
17+
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface;
18+
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface;
19+
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
20+
use Guzzle\Http\ClientInterface;
21+
use Symfony\Component\OptionsResolver\OptionsResolver;
22+
23+
/**
24+
* Symfony HttpCache invalidator.
25+
*
26+
* @author David de Boer <[email protected]>
27+
* @author David Buchmann <[email protected]>
28+
*/
29+
class Symfony extends AbstractProxyClient implements PurgeInterface, RefreshInterface
30+
{
31+
const HTTP_METHOD_REFRESH = 'GET';
32+
33+
/**
34+
* The options configured in the constructor argument or default values.
35+
*
36+
* @var array
37+
*/
38+
private $options;
39+
40+
/**
41+
* {@inheritDoc}
42+
*
43+
* When creating the client, you can configure options:
44+
*
45+
* - purge_method: HTTP method that identifies purge requests.
46+
*
47+
* @param array $options The purge_method that should be used.
48+
*/
49+
public function __construct(array $servers, $baseUrl = null, ClientInterface $client = null, array $options = array())
50+
{
51+
parent::__construct($servers, $baseUrl, $client);
52+
53+
$resolver = new OptionsResolver();
54+
$resolver->setDefined(array('purge_method'));
55+
$resolver->setDefaults(array(
56+
'purge_method' => PurgeSubscriber::DEFAULT_PURGE_METHOD,
57+
));
58+
59+
$this->options = $resolver->resolve($options);
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function purge($url, array $headers = array())
66+
{
67+
$this->queueRequest($this->options['purge_method'], $url, $headers);
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function refresh($url, array $headers = array())
76+
{
77+
$headers = array_merge($headers, array('Cache-Control' => 'no-cache'));
78+
$this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers);
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
protected function getAllowedSchemes()
87+
{
88+
return array('http', 'https');
89+
}
90+
}

src/SymfonyCache/PurgeSubscriber.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*/
2828
class PurgeSubscriber extends AccessControlledSubscriber
2929
{
30+
const DEFAULT_PURGE_METHOD = 'PURGE';
31+
3032
/**
3133
* The options configured in the constructor argument or default values.
3234
*
@@ -59,7 +61,7 @@ public function __construct(array $options = array())
5961
$resolver->setDefaults(array(
6062
'purge_client_matcher' => null,
6163
'purge_client_ips' => null,
62-
'purge_method' => 'PURGE',
64+
'purge_method' => static::DEFAULT_PURGE_METHOD,
6365
));
6466

6567
$this->options = $resolver->resolve($options);

0 commit comments

Comments
 (0)