New in Symfony 3.2: Tagged Cache

Contributed by
Nicolas Grekas
in #19047.

In Symfony 3.1 we added a new Cache component that was a strict implementation of the PSR-6: Caching Interface standard. In Symfony 3.2 we decided to improve the cache with some features not defined by the standard.

The first new feature is the tag-based invalidation to create tagged caches. Imagine that your application is an e-commerce application that stores users' reviews in the cache. When saving those reviews, you can now associate tags to them:

1
2
3
4
5
6
7
8
9
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cache = new FilesystemAdapter();

$review = $cache->getItem('reviews-'.$reviewId);
$review->set('...');
$review->tag(['reviews', 'products', 'product-'.$productId]);

$cache->save($review);

The cached review is associated with three different tags that can be used to invalidate related items:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// the HTML structure of reviews has changed:
// invalidate all reviews
$cache->invalidateTags('reviews');

// a special sale is enabled in the store:
// invalidate anything related to products
$cache->invalidateTags('products');

// the data of the product #123 has changed:
// invalidate anything related to that product
$cache->invalidateTags('product-123');

// a major store update is being deployed:
// invalidate all the information related to products and reviews
$cache->invalidateTags(['products', 'reviews']);

// after invalidating any of the previous tags, the item is no longer
// available in the cache:
$cache->getItem('reviews-'.$reviewId)->isHit();  // returns false

The Cache component now defines a TagAwareAdapterInterface to add tag-based invalidation to your own cache adapters and TaggedCacheItemInterface to allow tagging cache items. In addition, it includes a TagAwareRedisAdapter to enable tag-based invalidation when using Redis.

Comments

Great add, thanks !
I really love these new features, version after version... Thanks!
There are only two hard things in Computer Science: cache invalidation and naming things.

-- Phil Karlton

Thanks for this feature, it should make cache invalidation a little less hard!
I'd achieved something similar in the past with long cache key names and regexp cache cleanup, this is certainly a lot neater and easier to follow!
Will there be tag aware adapters other than redis included when 3.2 is released?
@Jeff, which one would you need?
Is the Symfony Http-Cache using this?
Well done!!
Pretty cool to see that Symfony walks in the PhpFastCache footsteps, Symfony is becoming a very cool lib scrapping :)
@Nicolas Grekas. Does tag aware adapter depend on certain feature(s) available on the backend or can all adapters be made tag aware?
As from Symfony 3.1 onwards, Apcu will automatically be used if available, assuming possible, would be great if Apcu adapter can be made tag aware.
Will the cache component have information included in Profiler?
@Jeff there is a new issue to discuss about how to integrate the Cache in the Profiler: https://github.com/symfony/symfony/issues/19297
Nice feature. No complex key naming just be allow their invalidation.
@Nicolas
Regarding our question which tag aware adapters: Is or will there be something for Varnish?
@Jeff Varnish is out of scope. About backbends for tags, most have the required primitive (atomic increments). Yet apcu may not be the best one, because of fragmentation incurred by tags invalidation, and also because shared backbends are the most useful (for invalidating all fronts at the same time). Still, anyone can submit their PRs 😃
@Nicolas I think 3.2 should also include tag aware Filesystem adapter. Filesystem can be used for development and redis can be used for production. Only need to change config.yml to switch.
@Nicolas Good work! An adapter to replace https://github.com/dantleech/sf-http-cache-tagging (which doesn't work with Symfony 3.X) would be brilliant - it's been a lifesaver on small sites which don't justify Varnish but still need a boost.

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.