The Cache Manager

Use the CacheManager to explicitly invalidate or refresh paths, URLs, routes or headers.

By invalidating a piece of content, you tell your caching proxy to no longer serve it to clients. When next requested, the proxy will fetch a fresh copy from the backend application and serve that instead.

By refreshing a piece of content, a fresh copy will be fetched right away.

Note

These terms are explained in more detail in An Introduction to Cache Invalidation.

invalidatePath

Important

Make sure to configure your proxy for purging first.

Invalidate a path:

$cacheManager->invalidatePath('/users')->flush();

Note

The flush() method is explained below.

Invalidate a URL:

$cacheManager->invalidatePath('http://www.example.com/users');

Invalidate a route:

$cacheManager = $container->get('fos_http_cache.cache_manager');
$cacheManager->invalidateRoute('user_details', array('id' => 123));

Invalidate a regular expression:

$cacheManager = $container->get('fos_http_cache.cache_manager');
$cacheManager->invalidateRegex('.*', 'image/png', array('example.com'));

The cache manager offers a fluent interface:

$cacheManager
    ->invalidateRoute('villains_index')
    ->invalidatePath('/bad/guys')
    ->invalidateRoute('villain_details', array('name' => 'Jaws')
    ->invalidateRoute('villain_details', array('name' => 'Goldfinger')
    ->invalidateRoute('villain_details', array('name' => 'Dr. No')
;

Refreshing

Note

Make sure to configure your proxy for purging first.

Refresh a path:

$cacheManager = $container->get('fos_http_cache.cache_manager');
$cacheManager->refreshPath('/users');

Refresh a URL:

$cacheManager = $container->get('fos_http_cache.cache_manager');
$cacheManager->refreshPath('http://www.example.com/users');

Refresh a Route:

$cacheManager = $container->get('fos_http_cache.cache_manager');
$cacheManager->refreshRoute('user_details', array('id' => 123));

tagResponse()

Use the Cache Manager to tag responses:

// $response is a \Symfony\Component\HttpFoundation\Response object
$cacheManager->tagResponse($response, array('some-tag', 'other-tag'));

The tags are appended to already existing tags, unless you set the $replace option to true:

$cacheManager->tagResponse($response, array('different'), true);

invalidateTags()

Invalidate cache tags:

$cacheManager->invalidateTags(array('some-tag', 'other-tag'));

Flushing

Internally, the invalidation requests are queued and only sent out to your HTTP proxy when the manager is flushed. The manager is flushed automatically at the right moment:

You can also flush the cache manager manually:

$cacheManager->flush();