Skip to content

[Cache] Cannot configure chain adapter #32139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Nyholm opened this issue Jun 23, 2019 · 6 comments
Closed

[Cache] Cannot configure chain adapter #32139

Nyholm opened this issue Jun 23, 2019 · 6 comments

Comments

@Nyholm
Copy link
Member

Nyholm commented Jun 23, 2019

Symfony version(s) affected: 3.4.x

Description
When trying to configure a cache ChainAdapter I get the following error:

Argument 1 passed to Symfony\Component\Cache\Adapter\ChainAdapter::__construct() must be of the type array, string given, called in /project/root/var/cache/dev/ContainerMdWoD6O/getCache_GlobalClearerService.php on line 20  

If I look in my container dump I see the following:

$this->services['cache.app'] = new \Symfony\Component\Cache\Adapter\ChainAdapter('y7VTFBCVw+', 2419200))

How to reproduce

composer create-project symfony/skeleton cache-issue

Add the following contents to cache.yaml:

framework:
    cache:
        # Put the unique name of your app here: the prefix seed
        # is used to compute stable namespaces for cache keys.
        #prefix_seed: your_vendor_name/app_name
        app: cache.chain
        pools:
            cache.chain:
                adapter: app.cache.adapter.chain

            cache.array:
                adapter: cache.adapter.array

            cache.apcu:
                adapter: cache.adapter.apcu


services:
    app.cache.adapter.chain:
        class: Symfony\Component\Cache\Adapter\ChainAdapter
        arguments:
            - [ '@cache.array', '@cache.apcu']
            - 2419200 # One month

Possible Solution
I've debugged some but I cannot figure out a good solution nor the cause.

@dmaicher
Copy link
Contributor

Seems this is related to this one?

#26954

@Nyholm
Copy link
Member Author

Nyholm commented Jun 23, 2019

Thank you.

Yes. I think so. I’ll test and confirm. I’ll make sure to update the docs if fixed.

@Nyholm
Copy link
Member Author

Nyholm commented Jun 23, 2019

Hm.. There is definitely some weird behaviour here:

This is my (semi) working example

framework:
    cache:
        pools:
            cache.array:
                adapter: cache.adapter.array

            cache.apcu:
                adapter: cache.adapter.apcu

services:
    cache.app:
        class: Symfony\Component\Cache\Adapter\ChainAdapter
        arguments:
            - [ '@cache.array', '@cache.apcu']
            - 2419200 # One month

But if I try to add the cache.pool tag to my cache.app service then I'll get the same error.

Argument 1 passed to Symfony\Component\Cache\Adapter\ChainAdapter::__construct() must be of the type array, string given,

This is indeed because of CachePoolPass line 109

This would be fine (but strange).. however, other services are using cache.app as their parent. And because of that I get an error again.

@Nyholm
Copy link
Member Author

Nyholm commented Jun 24, 2019

So with some help from Symfony Slack, I now know the following:

  • Not all adapters support being tagged with cache.pool
  • ChainAdapter is one of those pools.
  • You can decorate the pool with ProxyAdapter to make sure it can be tagged.

So, I can do something like this:

framework:
    cache:
        app: cache.chain
        pools:
             cache.chain:
                adapter: cache.adapter.psr6 # ProxyAdapter
                provider: my_chain_provider

            cache.array:
                adapter: cache.adapter.array

            cache.apcu:
                adapter: cache.adapter.apcu

services:
    my_chain_provider:
        class: Symfony\Component\Cache\Adapter\ChainAdapter
        arguments:
            - [ '@cache.array', '@cache.apcu']
            - 2419200 # One month

I've updated the PR to the docs: symfony/symfony-docs#11813

Thank you for the help.

@Nyholm Nyholm closed this as completed Jun 24, 2019
javiereguiluz added a commit to symfony/symfony-docs that referenced this issue Jun 25, 2019
…yholm)

This PR was submitted for the 4.3 branch but it was squashed and merged into the 3.4 branch instead (closes #11813).

Discussion
----------

[Cache] Make sure the chain cache configuration works

I just tried the configuration and noticed a few issues.

~~Im not 100% sure this actually works.~~ Lets wait for symfony/symfony#32139

Im sure now

Commits
-------

9fbebaf [Cache] Make sure the chain cache configuration works
@Nerogee
Copy link

Nerogee commented Sep 22, 2020

I created the cache chain with cache.adapter.redis as my first choice and cache.adapter.array the second. I expected this chain setting will auto switch to cache.adapter.array if redis server is not available. But symfony throws error when I shut down the redis server. What I'm doing here. (symfony 5.1)

framework:
    cache:
        pools:
            custom.app.cache:
                adapter: cache.adapter.psr6
                provider: app.my_chain_provider
                tags: true
            my.cache.redis:
                adapter: cache.adapter.redis
                tags: true
                provider: app.my_custom_redis_provider
            my.cache.array:
                adapter: cache.adapter.array

services:
    app.my_chain_provider:
        class: Symfony\Component\Cache\Adapter\ChainAdapter
        arguments:
            - ['@my.cache.redis', '@my.cache.array']
            - 2419200
    app.my_custom_redis_provider:
        class: \Redis
        factory: ['Symfony\Component\Cache\Adapter\RedisAdapter', 'createConnection']
        arguments:
            - 'redis://localhost'
            - { retry_interval: 2, timeout: 31536000 }

@nicolas-grekas
Copy link
Member

That's not what a chain is for. Please use support channels if you need help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants