|
| 1 | +Response Tagging |
| 2 | +================ |
| 3 | + |
| 4 | +This library provides a helper to handle tagging of responses ``ResponseTagger`` |
| 5 | +to simplify use of tags. The ``ResponseTagger`` helps you keep track tags for a response, which can be |
| 6 | +added to response headers that you can later use to invalidate all cache |
| 7 | +entries with that tag. |
| 8 | + |
| 9 | +.. _tags: |
| 10 | + |
| 11 | +Setup |
| 12 | +~~~~~ |
| 13 | + |
| 14 | +.. note:: |
| 15 | + |
| 16 | + Make sure to :doc:`configure your proxy <proxy-configuration>` for tagging first. |
| 17 | + |
| 18 | +The response tagger is a decorator around a proxy client that implements |
| 19 | +the ``TagsInterface``, handling adding tags to responses:: |
| 20 | + |
| 21 | + use FOS\HttpCache\ResponseTagger; |
| 22 | + |
| 23 | + // $proxyClient already created, implementing FOS\HttpCache\ProxyClient\Invalidation\TagsInterface |
| 24 | + $responseTagger = new ResponseTagger($proxyClient); |
| 25 | + |
| 26 | +Usage |
| 27 | +~~~~~ |
| 28 | + |
| 29 | +With tags you can group related representations so it becomes easier to |
| 30 | +invalidate them. You will have to make sure your web application adds the |
| 31 | +correct tags on all responses. You can add tags to the response using:: |
| 32 | + |
| 33 | + $responseTagger->addTags(['tag-two', 'group-a']); |
| 34 | + |
| 35 | +Before any content is sent out, you need to send the tag header_:: |
| 36 | + |
| 37 | + header(sprintf('%s: %s'), |
| 38 | + $responseTagger->getTagsHeaderName(), |
| 39 | + $responseTagger->getTagsHeaderValue() |
| 40 | + ); |
| 41 | + |
| 42 | +.. tip:: |
| 43 | + |
| 44 | + If you are using Symfony with the FOSHttpCacheBundle_, the tag header is |
| 45 | + set automatically. You also have `additional methods of defining tags`_ with |
| 46 | + annotations and on URL patterns. |
| 47 | + |
| 48 | +Assume you sent four responses: |
| 49 | + |
| 50 | ++------------+-------------------------+ |
| 51 | +| Response: | ``X-Cache-Tags`` header:| |
| 52 | ++============+=========================+ |
| 53 | +| ``/one`` | ``tag-one`` | |
| 54 | ++------------+-------------------------+ |
| 55 | +| ``/two`` | ``tag-two, group-a`` | |
| 56 | ++------------+-------------------------+ |
| 57 | +| ``/three`` | ``tag-three, group-a`` | |
| 58 | ++------------+-------------------------+ |
| 59 | +| ``/four`` | ``tag-four, group-b`` | |
| 60 | ++------------+-------------------------+ |
| 61 | + |
| 62 | +You can now invalidate some URLs using tags:: |
| 63 | + |
| 64 | + $tagHandler->invalidateTags(['group-a', 'tag-four'])->flush(); |
| 65 | + |
| 66 | +This will ban all requests having either the tag ``group-a`` /or/ ``tag-four``. |
| 67 | +In the above example, this will invalidate ``/two``, ``/three`` and ``/four``. |
| 68 | +Only ``/one`` will stay in the cache. |
| 69 | + |
| 70 | +.. note:: |
| 71 | + |
| 72 | + For further reading on tag invalidation see :doc:`cache-invalidator page <cache-invalidator>`. |
| 73 | + For changing the cache header, :doc:`configure your proxy <proxy-clients>`. |
| 74 | + |
| 75 | +.. _header: http://php.net/header |
| 76 | +.. _additional methods of defining tags: http://foshttpcachebundle.readthedocs.org/en/latest/features/tagging.html |
0 commit comments