Skip to content

Commit

Permalink
Merge pull request #131 from usemarkup/master
Browse files Browse the repository at this point in the history
Add headers as optional params while invalidating/refreshing
  • Loading branch information
ddeboer committed Aug 13, 2014
2 parents 2267000 + 395b250 commit 2be7986
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
10 changes: 6 additions & 4 deletions CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ public function tagResponse(Response $response, array $tags, $replace = false)
*
* @param string $name Route name
* @param array $parameters Route parameters (optional)
* @param array $headers Extra HTTP headers to send to the caching proxy (optional)
*
* @return $this
*/
public function invalidateRoute($name, array $parameters = array())
public function invalidateRoute($name, array $parameters = array(), array $headers = array())
{
$this->invalidatePath($this->urlGenerator->generate($name, $parameters));
$this->invalidatePath($this->urlGenerator->generate($name, $parameters), $headers);

return $this;
}
Expand All @@ -89,12 +90,13 @@ public function invalidateRoute($name, array $parameters = array())
*
* @param string $route Route name
* @param array $parameters Route parameters (optional)
* @param array $headers Extra HTTP headers to send to the caching proxy (optional)
*
* @return $this
*/
public function refreshRoute($route, array $parameters = array())
public function refreshRoute($route, array $parameters = array(), array $headers = array())
{
$this->refreshPath($this->urlGenerator->generate($route, $parameters));
$this->refreshPath($this->urlGenerator->generate($route, $parameters), $headers);

return $this;
}
Expand Down
13 changes: 12 additions & 1 deletion Resources/doc/features/invalidation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Cache Manager
-------------

To invalidate single paths, URLs and routes manually, use the
``invalidatePath($path)`` and ``invalidateRoute($route, $params)`` methods on
``invalidatePath($path, $headers)`` and ``invalidateRoute($route, $params, $headers)`` methods on
the cache manager::

$cacheManager = $container->get('fos_http_cache.cache_manager');
Expand All @@ -32,6 +32,10 @@ the cache manager::
// Invalidate a route
$cacheManager->invalidateRoute('user_details', array('id' => 123))->flush();

// Invalidate a route or path with headers
$cacheManager->invalidatePath('/users', array('X-Foo' => 'bar'))->flush();
$cacheManager->invalidateRoute('user_details', array('id' => 123), array('X-Foo' => 'bar'))->flush();

To invalidate multiple representations matching a regular expression, call
``invalidateRegex($path, $contentType, $hosts)``::

Expand All @@ -41,6 +45,13 @@ To refresh paths and routes, you can use ``refreshPath($path)`` and
``refreshRoute($route, $params)`` in a similar manner. See
:doc:`/reference/cache-manager` for more information.


By default, the proxy clients instantiate a `Guzzle client`_ to communicate
with the caching proxy. If you need to customize the requests, for example to
send a basic authentication header, you can inject a custom Guzzle client::
See the
:doc:`/reference/configuration/proxy-client#custom-guzzle-client` configuration reference.

.. _invalidation configuration:

Configuration
Expand Down
8 changes: 6 additions & 2 deletions Tests/Unit/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public function setUp()
public function testInvalidateRoute()
{
$httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface')
->shouldReceive('purge')->once()->with('/my/route')
->shouldReceive('purge')->once()->with('/route/with/params/id/123')
->shouldReceive('purge')->once()->with('/my/route', array())
->shouldReceive('purge')->once()->with('/route/with/params/id/123', array())
->shouldReceive('purge')->once()->with('/route/with/params/id/123', array('X-Foo' => 'bar'))
->shouldReceive('flush')->once()
->getMock();

Expand All @@ -46,6 +47,7 @@ public function testInvalidateRoute()

$cacheManager->invalidateRoute('my_route')
->invalidateRoute('route_with_params', array('id' => 123))
->invalidateRoute('route_with_params', array('id' => 123), array('X-Foo' => 'bar'))
->flush();
}

Expand All @@ -54,6 +56,7 @@ public function testRefreshRoute()
$httpCache = \Mockery::mock('\FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface')
->shouldReceive('refresh')->once()->with('/my/route', null)
->shouldReceive('refresh')->once()->with('/route/with/params/id/123', null)
->shouldReceive('refresh')->once()->with('/route/with/params/id/123', array('X-Foo' => 'bar'))
->shouldReceive('flush')->never()
->getMock();

Expand All @@ -72,6 +75,7 @@ public function testRefreshRoute()
$cacheManager
->refreshRoute('my_route')
->refreshRoute('route_with_params', array('id' => 123))
->refreshRoute('route_with_params', array('id' => 123), array('X-Foo' => 'bar'))
;
}

Expand Down

0 comments on commit 2be7986

Please sign in to comment.