January 11, 2017 Javier Eguiluz

The Symfony Cache component includes several adapters to support different caching mechanisms such as Redis, APCu, the filesystem, etc. In Symfony 3.3, we added a new adapter for Memcached.

When using it as a component, create first the connection to the Memcached server and then instantiate the new adapter:

1 2 3 4 use Symfony\Component\Cache\Adapter\MemcachedAdapter ; $client = MemcachedAdapter :: createConnection ( 'memcached://localhost' ); $cache = new MemcachedAdapter ( \Memcached $client , $namespace = '' , $defaultLifetime = 0 );

In addition to simple servers, the connection can also be a cluster of Memcached instances with all kinds of custom configuration:

When used in a Symfony application, it's even simpler to configure and use Memcached:

1 2 3 4 5 6 7 8 9 10 11 # app/config/config_prod.yml framework : cache : # defaults to memcached://localhost default_memcached_provider : "memcached://my.server.com:11211" # ... pools : app.cache.products : adapter : cache.adapter.memcached public : true # ...

Now you can start storing and fetching items in your Memcached-based cache: