New in Symfony 3.3: Memcached Cache Adapter
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Rob Frawley
and Nicolas Grekas
in #20858 and
#21108.
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:
1 2 3 4 5 6 7 8 9
$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'
));
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:
1 2 3 4 5 6 7 8
$cacheProduct = $this->get('app.cache.products')->getItem($productId);
if (!$cacheProduct->isHit()) {
$product = ...
$cacheProduct->set($product);
$this->get('app.cache.products')->save($cacheProduct);
} else {
$product = $cacheProduct->get();
}
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
Of course this doesn't mean that your comment is wrong, but we decided that the pros of doing this are more than that cons. Thanks for understanding it.
We hadn't always used the AppBundle in the past. That was a change we made along the way to improve things. So couldn't we expect to see other improvement to the doc standards in the future?
That said, Symfony docs are still world-class. And I hope they grow with the framework to stay on top.
All pros described in the doc to this bundle.