✍️ How to create clean Symfony cache pool

There is one thing I never found in the official symfony/cache documentation: how to create my own custom cache pool for my bundle (or my application).

I even created my own cache implementation (based on Symfony\Component\Cache\Adapter\FilesystemAdapter) !

But thanks to @stof who made me discover the following code sample. I saw how to make theses cache pool and wanted to share it !

So with the dependencyInjection:

YAML

services:
  my_bundle.cache:
    parent: cache.system
    public: false
    tags:
      - { name: cache.pool }

PHP

use Symfony\Component\DependencyInjection\ChildDefinition;

$definition = new ChildDefinition('cache.system');
$definition->setPublic(false);
$definition->addTag('cache.pool');
$container->setDefinition('my_bundle.pool', $definition);

Usage

After that you just have to use usual service injection. It will returns you a PSR-6 compatible object.

Clearing pool

By the way, you can also clear your pool with both:

$ php bin/console cache:clear

This one will clear all Symfony cache

$ php bin/console cache:pool:clear my_bundle.cache

And this one will clear only pool cache