-
Notifications
You must be signed in to change notification settings - Fork 61
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
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() | ||
; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
-------------------------------------- | ||
|
||
|
This file was deleted.
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:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
this is mentioned in usage part already There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -47,6 +48,11 @@ class CacheInvalidator | |
*/ | ||
const INVALIDATE = 'invalidate'; | ||
|
||
/** | ||
* Value to check support of invalidateTags operation. | ||
*/ | ||
const TAGS = 'tags'; | ||
|
||
/** | ||
* @var ProxyClientInterface | ||
*/ | ||
|
@@ -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); | ||
} | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra blank line