Skip to content

Add abstracted tag handling #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpC
caching proxy.
* Refactored the proxy client test system into traits. Removed ProxyTestCase,
use the traits `CacheAssertions` and `HttpCaller` instead.
* Abstracting tags by adding new `TagsInterface` for ProxyClients, as part of that also:
BC break: Moved tag invalidation to `CacheInvalidator`, and rename TagHandler to ResponseTagger.

1.4.0
-----
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ This library integrates your PHP applications with HTTP caching proxies such as
Use this library to send invalidation requests from your application to the caching proxy
and to test your caching and invalidation code against a Varnish setup.

It does this by abstracting some caching concepts and attempting to make sure these
can be supported across Varnish, Nginx and Symfony HttpCache.

If you use Symfony2, have a look at the
[FOSHttpCacheBundle](https://github.com/FriendsOfSymfony/FOSHttpCacheBundle).
The bundle provides the invalidator as a service, along with a number of
Expand All @@ -22,6 +25,7 @@ Features

* Send [cache invalidation requests](http://foshttpcache.readthedocs.org/en/stable/cache-invalidator.html)
with minimal impact on performance.
* Cache tagging abstraction, uses BAN with Varnish and allows tagging support for other caching proxies in the future.
* Use the built-in support for [Varnish](http://foshttpcache.readthedocs.org/en/stable/varnish-configuration.html)
3 and 4, [NGINX](http://foshttpcache.readthedocs.org/en/stable/nginx-configuration.html), the
[Symfony reverse proxy from the http-kernel component](http://foshttpcache.readthedocs.org/en/stable/symfony-cache-configuration.html)
Expand Down
26 changes: 26 additions & 0 deletions doc/cache-invalidator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ Refresh a URL with added header(s)::

.. _invalidate regex:


Invalidating by Tags
--------------------

.. note::

Make sure to :doc:`configure your proxy <proxy-configuration>` for tagging first,
in the case of Varnish this is powered by banning.

When you are using :doc:`response tagging <response-tagging>`, you can invalidate
all responses that where tagged with a specific label.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra blank line

Invalidate a tag::

$cacheInvalidator->invalidateTags(['blog-post-44'])->flush();

See below for the :ref:`flush() <flush>` method.

Invalidate several tags::

$cacheInvalidator
->invalidateTags(['type-65', 'location-3'])
->flush()
;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we layout this as:

$cacheInvalidator
    ->invalidateTags(['type-65', 'location-3'])
   ->flush()
;



Invalidating With a Regular Expression
--------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Contents:
proxy-configuration
proxy-clients
cache-invalidator
invalidation-handlers
response-tagging
user-context

testing-your-application
Expand Down
98 changes: 0 additions & 98 deletions doc/invalidation-handlers.rst

This file was deleted.

10 changes: 10 additions & 0 deletions doc/proxy-clients.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ include that port in the base URL::

$varnish = new Varnish($servers, 'my-cool-app.com:8080');

.. _varnish_custom_tags_header:

Another optional parameter available on Varnish client is ``tagsHeader``, which allows you to
change the default HTTP header used for tagging, ``X-Cache-Tags``::

$varnish = new Varnish($servers, 'example.com', $adapter, 'X-Custom-Tags-Header');

Make sure to reflect this change in your :doc:`caching proxy configuration <proxy-configuration>`.

.. note::

To make invalidation work, you need to :doc:`configure Varnish <varnish-configuration>` accordingly.
Expand Down Expand Up @@ -267,5 +276,6 @@ Varnish client::
Make sure to add any headers that you want to ban on to your
:doc:`proxy configuration <proxy-configuration>`.

.. _header: http://php.net/header
.. _HTTP Adapter: http://php-http.readthedocs.org/en/latest/
.. _PSR-7 message implementation: https://packagist.org/providers/psr/http-message-implementation
76 changes: 76 additions & 0 deletions doc/response-tagging.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
Response Tagging
================

The ``ResponseTagger`` helps you keep track tags for a response, which can be
added to response headers that you can later use to invalidate all cache
entries with that tag.

.. _tags:

Setup
~~~~~

.. note::

Make sure to :doc:`configure your proxy <proxy-configuration>` for tagging first.

The response tagger is a decorator around a proxy client that implements
the ``TagsInterface``, handling adding tags to responses::

use FOS\HttpCache\ResponseTagger;

// $proxyClient already created, implementing FOS\HttpCache\ProxyClient\Invalidation\TagsInterface
$responseTagger = new ResponseTagger($proxyClient);

Usage
~~~~~

With tags you can group related representations so it becomes easier to
invalidate them. You will have to make sure your web application adds the
correct tags on all responses. You can add tags to the response using::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not true, the tags are added into the handler (maybe call it ResponseTagger here). but to add them to the response, you need to use getTagsHeaderName|Value. or with #230 call tagResponse to tag a PSR-7 response. but the response is not tagged simply by calling this.

this is different in the symfony bundle, which we might add a note about somewhere here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is different in the symfony bundle, which we might add a note about somewhere here.

this is mentioned in usage part already

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, perfect. but lets talk about the ResponseTagger here, not about adding to the response, because that could lead to assume that the tags would magically happen to be in the resposne without doing anything :-)


$responseTagger->addTags(['tag-two', 'group-a']);

Before any content is sent out, you need to send the tag header_::

header(sprintf('%s: %s'),
$responseTagger->getTagsHeaderName(),
$responseTagger->getTagsHeaderValue()
);

.. tip::

If you are using Symfony with the FOSHttpCacheBundle_, the tags
added to ``ResponseTagger`` are added to the response automatically.
You also have `additional methods of defining tags`_ with
annotations and on URL patterns.

Assume you sent four responses:

+------------+-------------------------+
| Response: | ``X-Cache-Tags`` header:|
+============+=========================+
| ``/one`` | ``tag-one`` |
+------------+-------------------------+
| ``/two`` | ``tag-two, group-a`` |
+------------+-------------------------+
| ``/three`` | ``tag-three, group-a`` |
+------------+-------------------------+
| ``/four`` | ``tag-four, group-b`` |
+------------+-------------------------+

You can now invalidate some URLs using tags::

$tagHandler->invalidateTags(['group-a', 'tag-four'])->flush();

This will ban all requests having either the tag ``group-a`` /or/ ``tag-four``.
In the above example, this will invalidate ``/two``, ``/three`` and ``/four``.
Only ``/one`` will stay in the cache.

.. note::

For further reading on tag invalidation see :doc:`cache-invalidator page <cache-invalidator>`.
For changing the cache header, :doc:`configure your proxy <proxy-clients>`.

.. _header: http://php.net/header
.. _additional methods of defining tags: http://foshttpcachebundle.readthedocs.org/en/latest/features/tagging.html
2 changes: 1 addition & 1 deletion doc/varnish-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ using an ``X-Cache-Tags`` header.
If you need to use a different tag for the headers than the default
``X-Cache-Tags`` used in ``fos_ban.vcl``, you will have to write your own VCL
code for tag invalidation and change the tagging header
:ref:`configured in the cache invalidator <custom_tags_header>`. Your custom
:ref:`configured in the cache invalidator <varnish_custom_tags_header>`. Your custom
VCL will look like this:

.. configuration-block::
Expand Down
31 changes: 30 additions & 1 deletion src/CacheInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@
use FOS\HttpCache\Exception\ProxyUnreachableException;
use FOS\HttpCache\Exception\UnsupportedProxyOperationException;
use FOS\HttpCache\ProxyClient\ProxyClientInterface;
use FOS\HttpCache\ProxyClient\Invalidation\TagsInterface;
use FOS\HttpCache\ProxyClient\Invalidation\BanInterface;
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface;
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Manages HTTP cache invalidation.
*
* @author David de Boer <[email protected]>
* @author David Buchmann <[email protected]>
* @author André Rømcke <[email protected]>
*/
class CacheInvalidator
{
Expand All @@ -47,6 +48,11 @@ class CacheInvalidator
*/
const INVALIDATE = 'invalidate';

/**
* Value to check support of invalidateTags operation.
*/
const TAGS = 'tags';

/**
* @var ProxyClientInterface
*/
Expand Down Expand Up @@ -90,6 +96,8 @@ public function supports($operation)
return $this->cache instanceof RefreshInterface;
case self::INVALIDATE:
return $this->cache instanceof BanInterface;
case self::TAGS:
return $this->cache instanceof TagsInterface;
default:
throw new InvalidArgumentException('Unknown operation ' . $operation);
}
Expand Down Expand Up @@ -197,6 +205,27 @@ public function invalidate(array $headers)
return $this;
}

/**
* Remove/Expire cache objects based on cache tags
*
* @see TagsInterface::tags()
*
* @param array $tags Tags that should be removed/expired from the cache
*
* @throws UnsupportedProxyOperationException If HTTP cache does not support Tags invalidation
*
* @return $this
*/
public function invalidateTags(array $tags)
{
if (!$this->cache instanceof TagsInterface) {
throw UnsupportedProxyOperationException::cacheDoesNotImplement('Tags');
}
$this->cache->invalidateTags($tags);

return $this;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please have an empty line before the return statement for readability.

}

/**
* Invalidate URLs based on a regular expression for the URI, an optional
* content type and optional limit to certain hosts.
Expand Down
Loading