在 Symfony 3.2 中根据 config.yml 使用 TagAwareAdapter?

LBA*_*LBA 4 php symfony

我知道我可以在 Symfony 3.2 中定义缓存,就像在 config.yml 中一样:

    cache:
    default_redis_provider: redis://%redis_password%@%redis_host%:%redis_port%
    pools:
        my_redis_cache:
            adapter: cache.adapter.redis
            public: true
            default_lifetime: 1200
            provider: cache.default_redis_provider
Run Code Online (Sandbox Code Playgroud)

例如在我的控制器中我可以简单地使用

$cache = $this->get('my_redis_cache');
Run Code Online (Sandbox Code Playgroud)

现在,从 Symfony 3.2 开始,我们有了一个名为TagAwareAdapter的奇妙新功能- 它允许通过标签使缓存失效。

这是通过以下代码完成的:

$cache = new TagAwareAdapter(
// Adapter for cached items
new FilesystemAdapter(),
// Adapter for tags
new RedisAdapter('redis://localhost')
);
Run Code Online (Sandbox Code Playgroud)

但我可以在 config.yml 中定义它吗?

我正在寻找类似的东西:

    cache:
    default_redis_provider: redis://%redis_password%@%redis_host%:%redis_port%
    pools:
        my_redis_cache:
            adapter: cache.adapter.redis
            public: true
            default_lifetime: 1200
            provider: cache.default_redis_provider
        my_tag_aware_cache:
            adapter: cache.adapter.tagawareadapter
            provider:
                - my_file_cache
                - my_redis_cache
Run Code Online (Sandbox Code Playgroud)

但我不知道如何定义它 - 我已经玩尝试和错误有一段时间了 - 到目前为止我得到的只是错误。

klo*_*oma 5

上面的答案提供了解决方案,并且它是一个很好的解决方案,我将其全部放在一起作为答案,因为我还浪费了一些时间来了解如何使用 TagAwareAdapter。这是我针对 Symfony 3.4 的解决方案。

配置.yml

cache:
    pools:
        app.cache.file:
            public: true
            adapter: cache.adapter.filesystem
        app.cache.redis:
            public: true
            adapter: cache.adapter.redis
Run Code Online (Sandbox Code Playgroud)

服务.yml

myapp.cache:
    public: true
    class: Symfony\Component\Cache\Adapter\TagAwareAdapter
    arguments: [ '@app.cache.file', '@app.cache.redis' ]
Run Code Online (Sandbox Code Playgroud)

像这样使用它(例如在控制器中):

/** @var TagAwareAdapterInterface $cache */
$cache = $this->get('myapp.cache');
Run Code Online (Sandbox Code Playgroud)

不要忘记有一个 redis 实例并安装 redis 包:

composer require predis/predis
Run Code Online (Sandbox Code Playgroud)