Skip to content

Commit 3805394

Browse files
authored
Merge pull request #366 from FriendsOfSymfony/adjust-tag-header-configuration
Adjust to tag header cleanup and allow more configuration on varnish proxy client
2 parents 52c1a59 + c7b9938 commit 3805394

File tree

19 files changed

+153
-83
lines changed

19 files changed

+153
-83
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ Changelog
4343
CacheManager::invalidateTags (if you use annotations for tag invalidation, you
4444
don't need to change anything). Recording tags and writing them into the
4545
responses is now done through the SymfonyResponseTagger.
46+
The service `fos_http_cache.handler.tag_handler` no longer exists. For
47+
tagging responses, use `fos_http_cache.http.symfony_response_tagger` instead,
48+
and to invalidate tags use the service `fos_http_cache.cache_manager`.
49+
* **BC break:** The configuration `tags.header` has been removed. Configuring
50+
the header for tagging responses is now done at `tags.response_header`.
51+
Configuring the header for tag invalidation requests is now done at
52+
`proxy_client.varnish.tags_header`.
4653

4754
### Tests
4855

Resources/doc/features/tagging.rst

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,56 +34,71 @@ For more information, see :doc:`/reference/configuration/tags`.
3434
Setting and Invalidating Tags
3535
-----------------------------
3636

37-
You can tag responses in different ways: with the tag handler from PHP or with
38-
a twig function, from configuration or using annotations on controller actions.
37+
You can tag responses in different ways:
38+
39+
* From PHP code by using the response tagger to set tags and the cache manager
40+
to invalidate tags;
41+
* Set tags from twig templates with a function;
42+
* In project configuration or using annotations on controller actions.
43+
44+
You can add tags before the response object exists. The tags are automatically
45+
added to the response by a listener. The listener also detects pending tag
46+
invalidations and flushes them. As with other invalidation operations, tag
47+
invalidation requests are flushed to the caching proxy
48+
:ref:`after the response has been sent <flushing>`.
3949

4050
Tagging from Code
4151
~~~~~~~~~~~~~~~~~
4252

43-
Inject the ``TagHandler`` (service ``fos_http_cache.handler.tag_handler``) and
44-
use ``addTags($tags)`` to add tags that will be set on the response::
53+
To add tags to responses, inject the ``ResponseTagger`` (service
54+
``fos_http_cache.http.symfony_response_tagger``) and use ``addTags($tags)`` to
55+
add tags that will be set on the response::
4556

46-
use FOS\HttpCacheBundle\Handler\TagHandler;
57+
use FOS\HttpCacheBundle\Http\SymfonyResponseTagger;
4758

4859
class NewsController
4960
{
5061
/**
51-
* @var TagHandler
62+
* @var SymfonyResponseTagger
5263
*/
53-
private $tagHandler;
64+
private $responseTagger;
5465

5566
public function articleAction($id)
5667
{
57-
$this->tagHandler->addTags(array('news', 'news-' . $id));
68+
$this->responseTagger->addTags(array('news', 'news-' . $id));
5869

5970
// ...
6071
}
6172
}
6273

63-
To invalidate tags, call ``TagHandler::invalidateTags($tags)``::
74+
To invalidate tags, inject the ``CacheManager`` (service ``fos_http_cache.cache_manager``)
75+
and call ``invalidateTags($tags)`` on it::
76+
77+
use FOS\HttpCacheBundle\CacheManager;
6478

6579
class NewsController
6680
{
67-
// ...
81+
/**
82+
* @var CacheManager
83+
*/
84+
private $cacheManager;
6885

6986
public function editAction($id)
7087
{
7188
// ...
7289

73-
$this->tagHandler->invalidateTags(array('news-' . $id));
90+
$this->cacheManager->invalidateTags(array('news-' . $id));
7491

7592
// ...
7693
}
7794
}
7895

79-
See the :ref:`Tag Handler reference <tag_handler_addtags>` for full details.
80-
8196
Tagging from Twig Templates
8297
~~~~~~~~~~~~~~~~~~~~~~~~~~~
8398

8499
In situations where a page is assembled in the templating layer, it can be more
85100
convenient to add tags from inside the template. This works the same way as
86-
with the tag handler and can also be mixed with the other methods:
101+
with the response tagger and can also be mixed with the other methods:
87102

88103
.. code-block:: jinja
89104

Resources/doc/reference.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ annotations and public methods.
1010
reference/configuration
1111
reference/annotations
1212
reference/cache-manager
13-
reference/tag-handler
1413
reference/glossary

Resources/doc/reference/cache-manager.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
The Cache Manager
22
=================
33

4-
Use the CacheManager to explicitly invalidate or refresh paths, URLs, routes or
5-
headers.
4+
Use the CacheManager to explicitly invalidate or refresh paths, URLs, routes,
5+
tags or responses with specific headers.
66

77
By *invalidating* a piece of content, you tell your caching proxy to no longer
88
serve it to clients. When next requested, the proxy will fetch a fresh copy
@@ -82,12 +82,16 @@ Refresh a Route::
8282

8383
.. _cache_manager_tags:
8484

85-
``tagResponse()``, ``invalidateTags()``
86-
---------------------------------------
85+
``invalidateTags()``
86+
--------------------
87+
88+
Invalidate cache tags::
89+
90+
$cacheManager->invalidateTags(array('some-tag', 'other-tag'));
91+
92+
.. note::
8793

88-
.. versionadded:: 1.3
89-
Since version 1.3, use the :doc:`TagHandler <tag-handler>` instead of the
90-
CacheManager for working with tags.
94+
Marking a response with tags can be done through the :doc:`ResponseTagger <../features/tagging>`.
9195

9296
.. _flushing:
9397

Resources/doc/reference/configuration/proxy-client.rst

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,42 @@ varnish
2727
fos_http_cache:
2828
proxy_client:
2929
varnish:
30+
tags_header: My-Cache-Tags
31+
header_length: 1234
32+
default_ban_headers:
33+
Foo: Bar
3034
http:
3135
servers:
3236
- 123.123.123.1:6060
3337
- 123.123.123.2
3438
base_url: yourwebsite.com
3539
36-
``servers``
37-
"""""""""""
40+
``tags_header``
41+
"""""""""""""""
42+
43+
**type**: ``string`` **default**: ``X-Cache-Tags``
44+
45+
Header for sending tag invalidation requests to Varnish.
46+
47+
``header_length``
48+
"""""""""""""""""
49+
50+
**type**: ``integer`` **default**: ``7500``
51+
52+
Maximum header length when invalidating tags. If there are more tags to
53+
invalidate than fit into the header, the invalidation request is split into
54+
multiple requests.
55+
56+
``default_ban_headers``
57+
"""""""""""""""""""""""
58+
59+
**type**: ``array``
60+
61+
Map of header name header value that have to be set on each ban request. This
62+
list is merged with the built-in headers for bans.
63+
64+
``http.servers``
65+
""""""""""""""""
3866

3967
**type**: ``array``
4068

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

49-
``base_url``
50-
""""""""""""
77+
``http.base_url``
78+
"""""""""""""""""
5179

5280
**type**: ``string``
5381

Resources/doc/reference/tag-handler.rst

Lines changed: 0 additions & 28 deletions
This file was deleted.

Resources/doc/spelling_word_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ symfony
88
github
99
subdomains
1010
yaml
11+
invalidations
1112
invalidator
1213
ETag
1314
templating

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"require": {
2424
"php": "^5.6.0||^7.0.0",
25-
"friendsofsymfony/http-cache": "^2.0.0-beta1",
25+
"friendsofsymfony/http-cache": "2.0.x-dev as 2.0.0-beta3",
2626
"symfony/framework-bundle": "^2.8||^3.0",
2727
"symfony/http-foundation": "~2.8.13||^3.1.6"
2828
},

src/CacheManager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use FOS\HttpCache\CacheInvalidator;
1515
use FOS\HttpCache\ProxyClient\ProxyClient;
16-
use Symfony\Component\HttpFoundation\Response;
1716
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1817

1918
/**

src/DependencyInjection/Configuration.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace FOS\HttpCacheBundle\DependencyInjection;
1313

14+
use FOS\HttpCache\ProxyClient\Varnish;
15+
use FOS\HttpCache\TagHeaderFormatter\TagHeaderFormatter;
1416
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1517
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
1618
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
@@ -307,8 +309,30 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
307309
->info('If you configure more than one proxy client, you need to specify which client is the default.')
308310
->end()
309311
->arrayNode('varnish')
312+
->fixXmlConfig('default_ban_header')
313+
->validate()
314+
->always(function ($v) {
315+
if (!count($v['default_ban_headers'])) {
316+
unset($v['default_ban_headers']);
317+
}
318+
319+
return $v;
320+
})
321+
->end()
310322
->children()
311-
->append($this->getHttpDispatcherNode())
323+
->scalarNode('tags_header')
324+
->defaultValue(Varnish::DEFAULT_HTTP_HEADER_CACHE_TAGS)
325+
->info('HTTP header to use when sending tag invalidation requests to Varnish')
326+
->end()
327+
->scalarNode('header_length')
328+
->info('Maximum header length when invalidating tags. If there are more tags to invalidate than fit into the header, the invalidation request is split into several requests.')
329+
->end()
330+
->arrayNode('default_ban_headers')
331+
->useAttributeAsKey('name')
332+
->info('Map of additional headers to include in each ban request.')
333+
->prototype('scalar')->end()
334+
->end()
335+
->append($this->getHttpDispatcherNode())
312336
->end()
313337
->end()
314338

@@ -473,8 +497,8 @@ private function addTagSection(ArrayNodeDefinition $rootNode)
473497
->defaultNull()
474498
->info('Service name of a custom ExpressionLanugage to use.')
475499
->end()
476-
->scalarNode('header')
477-
->defaultValue('X-Cache-Tags')
500+
->scalarNode('response_header')
501+
->defaultValue(TagHeaderFormatter::DEFAULT_HEADER_NAME)
478502
->info('HTTP header that contains cache tags')
479503
->end()
480504
->arrayNode('rules')

0 commit comments

Comments
 (0)