Skip to content

Commit

Permalink
Merge pull request #209 from FriendsOfSymfony/symfony-cache-purge-ref…
Browse files Browse the repository at this point in the history
…resh

load purge and refresh handlers by default
  • Loading branch information
ddeboer committed Jun 4, 2015
2 parents 227ac2d + 4b004e6 commit db5d4b3
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 37 deletions.
13 changes: 6 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ Changelog
1.3.0
-----

* Configured/annotated cache tags on subrequests (in twig: `render(controller())`)
* Added configuration for Symfony HttpCache client and HttpCache now loads
purge and refresh handlers by default.
* Configured/annotated cache tags on subrequests (in Twig: `render(controller())`)
are no longer ignored. Additionally, it is now possible to add tags from code
before the response object has been created, by using the TagHandler, and from
twig with the `fos_httpcache_tag` function.

Twig with the `fos_httpcache_tag` function.
If you defined custom services for the `InvalidateTagCommand`, you should
now inject the TagHandler instead of the CacheManager.

**deprecated** `CacheManager::tagResponse` in favor of `TagHandler::addTags`

* **2015-05-08** Added configuration option for custom proxy client (#208)
* **deprecated** `CacheManager::tagResponse` in favor of `TagHandler::addTags`
* Added configuration option for custom proxy client (#208)
* Added support for a simple Etag header in the header configuration (#207)

1.2.0
Expand Down
24 changes: 23 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
->arrayNode('proxy_client')
->children()
->enumNode('default')
->values(array('varnish', 'nginx'))
->values(array('varnish', 'nginx', 'symfony'))
->info('If you configure more than one proxy client, specify which client is the default.')
->end()
->arrayNode('varnish')
Expand Down Expand Up @@ -356,6 +356,28 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
->end()
->end()

->arrayNode('symfony')
->fixXmlConfig('server')
->children()
->arrayNode('servers')
->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
->useAttributeAsKey('name')
->isRequired()
->requiresAtLeastOneElement()
->prototype('scalar')->end()
->info('Addresses of the hosts Symfony HttpCache is running on. May be hostname or ip, and with :port if not the default port 80.')
->end()
->scalarNode('base_url')
->defaultNull()
->info('Default host name and optional path for path based invalidation.')
->end()
->scalarNode('guzzle_client')
->defaultNull()
->info('Guzzle service to use for customizing the requests.')
->end()
->end()
->end()

->end()
->end()
->end();
Expand Down
33 changes: 27 additions & 6 deletions DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loa
if (isset($config['nginx'])) {
$this->loadNginx($container, $loader, $config['nginx']);
}
if (isset($config['symfony'])) {
$this->loadSymfony($container, $loader, $config['symfony']);
}

$container->setAlias(
$this->getAlias().'.default_proxy_client',
Expand All @@ -256,12 +259,8 @@ private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader,
}
$container->setParameter($this->getAlias().'.proxy_client.varnish.servers', $config['servers']);
$container->setParameter($this->getAlias().'.proxy_client.varnish.base_url', $baseUrl);
if ($config['guzzle_client']) {
$container->getDefinition($this->getAlias().'.proxy_client.varnish')
->addArgument(
new Reference($config['guzzle_client'])
)
;
if (!empty($config['guzzle_client'])) {
$container->setParameter($this->getAlias().'.proxy_client.varnish.guzzle_client', $config['guzzle_client']);
}
}

Expand All @@ -279,6 +278,28 @@ private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, a
$container->setParameter($this->getAlias().'.proxy_client.nginx.servers', $config['servers']);
$container->setParameter($this->getAlias().'.proxy_client.nginx.base_url', $baseUrl);
$container->setParameter($this->getAlias().'.proxy_client.nginx.purge_location', $config['purge_location']);
if (!empty($config['guzzle_client'])) {
$container->setParameter($this->getAlias().'.proxy_client.nginx.guzzle_client', $config['guzzle_client']);
}
}

private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config)
{
$loader->load('symfony-client.xml');
foreach ($config['servers'] as $url) {
$this->validateUrl($url, 'Not a valid web server address: "%s"');
}
if (!empty($config['base_url'])) {
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
$this->validateUrl($baseUrl, 'Not a valid base path: "%s"');
} else {
$baseUrl = null;
}
$container->setParameter($this->getAlias().'.proxy_client.symfony.servers', $config['servers']);
$container->setParameter($this->getAlias().'.proxy_client.symfony.base_url', $baseUrl);
if (!empty($config['guzzle_client'])) {
$container->setParameter($this->getAlias().'.proxy_client.symfony.guzzle_client', $config['guzzle_client']);
}
}

private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config)
Expand Down
1 change: 1 addition & 0 deletions Resources/config/nginx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<argument>%fos_http_cache.proxy_client.nginx.servers%</argument>
<argument>%fos_http_cache.proxy_client.nginx.base_url%</argument>
<argument>%fos_http_cache.proxy_client.nginx.purge_location%</argument>
<argument type="service" id="fos_http_cache.proxy_client.nginx.guzzle_client" on-invalid="ignore"/>
</service>

<service id="fos_http_cache.test.client.nginx"
Expand Down
20 changes: 20 additions & 0 deletions Resources/config/symfony.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="fos_http_cache.proxy_client.symfony.class">FOS\HttpCache\ProxyClient\Symfony</parameter>
</parameters>

<services>
<service id="fos_http_cache.proxy_client.symfony"
class="%fos_http_cache.proxy_client.symfony.class%">
<argument>%fos_http_cache.proxy_client.symfony.servers%</argument>
<argument>%fos_http_cache.proxy_client.symfony.base_url%</argument>
<argument type="service" id="fos_http_cache.proxy_client.symfony.guzzle_client" on-invalid="ignore"/>
</service>
</services>

</container>
1 change: 1 addition & 0 deletions Resources/config/varnish.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class="%fos_http_cache.proxy_client.varnish.class%">
<argument>%fos_http_cache.proxy_client.varnish.servers%</argument>
<argument>%fos_http_cache.proxy_client.varnish.base_url%</argument>
<argument type="service" id="fos_http_cache.proxy_client.varnish.guzzle_client" on-invalid="ignore"/>
</service>

<service id="fos_http_cache.test.client.varnish"
Expand Down
3 changes: 2 additions & 1 deletion Resources/doc/features/invalidation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Invalidation
**Works with**:

* :ref:`Varnish <foshttpcache:varnish configuration>`
* :ref:`Nginx <foshttpcache:nginx configuration>`
* :ref:`Nginx <foshttpcache:nginx configuration>` (except regular expressions)
* :doc:`symfony-http-cache` (except regular expressions)

**Preparation**:

Expand Down
7 changes: 5 additions & 2 deletions Resources/doc/features/symfony-http-cache.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ concept is to use event subscribers on the HttpCache class.

Symfony HttpCache support is currently limited to following features:

* Purge
* Refresh
* User context

Extending the correct HttpCache
Expand All @@ -34,7 +36,7 @@ Instead of extending ``Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache``, you
.. tip::

If your class already needs to extend a different class, simply copy the event
handling code from the EventDispatchingHttpCache into your ``AppCache`` class.
handling code from the ``EventDispatchingHttpCache`` into your ``AppCache`` class.
The drawback is that you need to manually check whether you need to adjust your
``AppCache`` each time you update the FOSHttpCache library.

Expand All @@ -43,7 +45,8 @@ about. You can disable subscribers, or customize how they are instantiated.

If you do not need all subscribers, or need to register some yourself to
customize their behavior, overwrite ``getOptions`` and return the right bitmap
in ``fos_default_subscribers``. Use the constants provided by the cache kernel::
in ``fos_default_subscribers``. Use the constants provided by the
``EventDispatchingHttpCache``::

public function getOptions()
{
Expand Down
4 changes: 3 additions & 1 deletion Resources/doc/features/tagging.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Tagging
=======

**Works with**: :ref:`Varnish <foshttpcache:varnish_tagging>`
**Works with**:

* :ref:`Varnish <foshttpcache:varnish_tagging>`

If your application has many intricate relationships between cached items,
which makes it complex to invalidate them by route, cache tagging will be
Expand Down
4 changes: 2 additions & 2 deletions Resources/doc/features/user-context.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ User Context

**Works with**:

- :ref:`Varnish <foshttpcache:varnish user context>`
- :doc:`symfony-http-cache`
* :ref:`Varnish <foshttpcache:varnish user context>`
* :doc:`symfony-http-cache`

If your application serves different content depending on the user's group
or context (guest, editor, admin), you can cache that content per user context.
Expand Down
2 changes: 2 additions & 0 deletions Resources/doc/reference/configuration/cache-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ specify the ``custom_proxy_client`` field.
Whether the cache manager service should be enabled. By default, it is enabled
if a proxy client is configured. It can not be enabled without a proxy client.

.. _custom_proxy_client:

``custom_proxy_client``
-----------------------

Expand Down
44 changes: 35 additions & 9 deletions Resources/doc/reference/configuration/proxy-client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ to work.

The proxy client is also directly available as a service
(``fos_http_cache.proxy_client.default`` and ``fos_http_cache.proxy_client.varnish``
or ``fos_http_cache.proxy_client.nginx``) that you can use directly.
, ``fos_http_cache.proxy_client.nginx`` or ``fos_http_cache.proxy_client.symfony``)
that you can use directly.

If you need to adjust the proxy client, you can also configure the ``CacheManager``
with a :ref:`custom proxy client <custom_proxy_client>` that you defined as a
service. In that case, you do not need to configure anything in the
``proxy_client`` configuration section.

varnish
-------
Expand All @@ -33,6 +39,10 @@ Comma-separated list of IP addresses or host names of your
caching proxy servers. The port those servers will be contacted
defaults to 80; you can specify a different port with ``:<port>``.

When using a multi-server setup, make sure to include **all** proxy servers in
this list. Invalidation must happen on all systems or you will end up with
inconsistent caches.

``base_url``
""""""""""""

Expand Down Expand Up @@ -83,10 +93,24 @@ Separate location that purge requests will be sent to.
See the :ref:`FOSHttpCache library docs <foshttpcache:nginx configuration>`
on how to configure Nginx.

symfony
-------

.. code-block:: yaml
# app/config/config.yml
fos_http_cache:
proxy_client:
symfony:
servers: 123.123.123.1:6060, 123.123.123.2
base_url: yourwebsite.com
For ``servers`` and ``base_url``, see above.

default
-------

**type**: ``enum`` **options**: ``varnish``, ``nginx``
**type**: ``enum`` **options**: ``varnish``, ``nginx``, ``symfony``

.. code-block:: yaml
Expand All @@ -95,27 +119,29 @@ default
proxy_client:
default: varnish
The default proxy client that will be used by the cache manager.
You can *use Nginx and Varnish in parallel*. If you need to cache and
invalidate pages in both, you can configure both in this bundle.
The cache manager however will only use the default client.
If there is only one proxy client, it is automatically the default. Only
configure this if you configured more than one proxy client.

The default proxy client that will be used by the cache manager. You can
*configure Nginx, Varnish and Symfony proxy clients in parallel*. There is
however only one cache manager and it will only use the default client.

Custom Guzzle Client
--------------------

By default, the proxy client instantiates a `Guzzle client`_ to talk with the
caching proxy. If you need to customize the requests, for example to send a
basic authentication header, you can configure a service and specify that in
the ``guzzle_client`` option. A sample service definition for using basic
authentication looks like this:
the ``guzzle_client`` option of any of the cache proxy clients. A sample
service definition for using basic authentication looks like this:

.. code-block:: yaml
# app/config/config.yml
acme.varnish.guzzle.client:
class: Guzzle\Service\Client
calls:
- [setDefaultOption, [auth, [%varnish.username%, %varnish.password%, basic ]]]
- [setDefaultOption, [auth, [%caching_proxy.username%, %caching_proxy.password%, basic ]]]
Caching Proxy Configuration
---------------------------
Expand Down
Loading

0 comments on commit db5d4b3

Please sign in to comment.