We have memcache on our Symfony 3.4 app:
cache:
app: cache.adapter.memcached
default_memcached_provider: "%app.memcached.dsn%"
However, we’ve been asked to use several cache servers, so just passing one DSN is no good.
Looking here (https://symfony.com/blog/new-in-symfony-3-3-memcached-cache-adapter), I see you can create it in code like this:
$client = MemcachedAdapter::createConnection(array(
// format => memcached://[user:pass@][ip|host|socket[:port]][?weight=int]
// 'weight' ranges from 0 to 100 and it's used to prioritize servers
'memcached://my.server.com:11211'
'memcached://rmf:abcdef@localhost'
'memcached://127.0.0.1?weight=50'
'memcached://username:the-password@/var/run/memcached.sock'
'memcached:///var/run/memcached.sock?weight=20'
));
However, that isn’t autowired.
I believe we need to either make a provider class, or somehow get it to make calls to addServer($dsn)
, once instantiated. I also saw the following on random posts:
memcache:
class: Memcached
calls:
- [ addServer, [ %app.memcached.dsn.1% ]]
- [ addServer, [ %app.memcached.dsn.2% ]]
However it isn’t really helping or I have missed something out.
Can anyone help? How do I create this provider class?
2
Answers
You can copy above code snippet as a service configuration to your services.yaml, which probably roughly looks like this:
Then in your configuration you should be able to reference the adapter using the client created by the factory, e.g. something like:
You might also be able to overwrite the default alias
cache.adapter.memcached
instead of having your own adapter.Your approach using
Memcached::addServer
might work as well, but just like withMemcachedAdapter::createConnection
this will return the Client, which needs to be passed to the cache adapter. That’s why there is a second serviceapp.memcached_adapter
, which is used in the cache configuration.Please be aware that I have not tested this, so this is rather a rough outline than a fully working solution,
For one of my projects running Symfony 3.4 the configuration was simpler:
Create a service that will be used as a client:
The
AppBundleServicesMemcached
can have all the custom logic I need like so:And then I used that service in my config.yml: