Memcache bundle for Symfony2

memcache_debug

Installation instructions can be found on the Github LswMemcacheBundle readme.

When building high traffic websites you probably heard about “Memcache”. If you want to optimize your web application for high load and/or fast page loads this is an indispensable tool. The memcache website states about the software that it is a:

“Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.” — memcached.org

One of the ways you can apply Memcache is as a session storage solution. It will manage your session data without doing disk I/O on web or database servers. You can also run it as a central object storage for your website. In this role it is used for caching expensive API calls or database queries.

PHP 5 has good support for Memcache using the “Memcache” and “Memcached” modules (note that there is a one-letter difference). The first module calls the Memcache daemon directly, while the second one uses “libmemcached” to communicate. You can see the difference between them in terms of features in a matrix on the Memcached wiki. We chose the “Memcached” module, because it offers more features, is newer and faster because it supports the binary protocol.

At LeaseWeb we have created a Symfony2 bundle with Web Debug Toolbar integration to help you optimize your web application performance. It is called ‘LswMemcacheBundle’ and can be found on Github and Packagist.

Share

7 thoughts on “Memcache bundle for Symfony2”

  1. I wouldn’t recommend memcached for session storage, because when the cache gets full it will throw the oldest sessions away whether or not they’re still in use. Generally speaking you should treat the contents of a cache as if they might go away at any time and only put things in them that you can rebuild. Probably fine if you have enough RAM set aside, but you never know.
    Anyway, cool, always nice to have another handy bundle 🙂

  2. @Matt: You are absolutely right. Altough on heavy traffic web farms it is very beneficial to use memcache session storage to bring down disk I/O, one should make sure that the sessions are not trown away. You could use different Memcache instances for the two different purposes (session storage and cache) and give the session instance enough RAM to prevent this issue. This is why I added support for multiple memcache instances to this bundle. Note that this bundle does not force you to use session storage in Memcache. If you do not set the session handler the normal session storage will be used and memcache can be used exclusively for caching. Thanks you for your good and very relevant feedback!

  3. Hi, I’would like to learn Memcached integration in Symfony2 where can i refer some material to understand basics and apply it into my project…?

  4. Hi! First of all, congratulations for your bundle, is fantastic. How can I set/get a doctrine object in the cache? I find one object in db and save it to memcache but when I want to retrieve it from cache I have the next error:

    “A new entity was found through the relationship that was not configured to cascade persist operations for entity.”

    Thanks you for your great work!

  5. @Pablo: Thank you! Glad you enjoy it. That is definitely not an error message from my bundle, so please add some code to show what you are doing, so that I can help you.

  6. Thanks for your answer! I think that the error may be due to an error at Doctrine because I have the error when I try to set and get a doctrine object directly to the cache. Yo can see the code below:

    Controller:

    $user = $this->getDoctrine()->getManager()->getRepository(‘MyBundle:User’)->find($user_id);

    $lifetime = 120;

    $cache = $this->get(‘memcache.default’);
    $cache->set(‘userAdmin’, user, lifetime);

    In other part of my code:

    $user = $cache->get(‘userAdmin’); //Here I have the error that I explain in my previous message

  7. @Pablo: Memcache serializes/unserializes objects to allow for (cache) storage. Serialization and deserialization is (AFAIK) not possible for Doctrine 2 objects. What you could do is implement a toArray() method on your entity object. Now you simply send and retrieve an array to and from Memcache. This is a simple and easy solution (converting to arrays is a concept called “hydration”). Note that hydration removes the flexibility an Entity has for querying related objects.

Leave a Reply

Your email address will not be published. Required fields are marked *