Earlier in the year, a new memcache extension was released for PHP by Andrei Zmievski, Core PHP Developer and Open Source Fellow at Digg.com. The extension is for dealing with memcache from PHP, and is called memcached (not to be confused with the existing extension, “memcache”). The idea behind the new extension is to leverage the functionality contained libmemcached – a solution which has been widely adopted across many scripting communities and beyond. The extension is available in PECL and has lots of shiny new functionality. This article looks at how to obtain and work with the new extension, and also shows off a couple of the new features it includes.

Installing Memcached

Memcached is available in PECL, so is straightforward to install and use. For me (on Ubuntu), I simply typed:

The “-beta” on the end is to tell pecl that its OK for it to grab a package not marked as stable – obviously once the package is marked as stable, this won’t be needed.

Migrating to Memcached

The new extension isn’t designed to have an identical API to the old one, so if you’re already using memcache there is no guarantee of being able to perform a straight swap. That said, I did exactly that with the example in my “Getting Started with Memcache” article, and it worked completely as expected with the only adjustmen being that addServer now requires the port number to connect.

In an ideal world, it would be possible to write a new caching class, swap out your old one, re-run your test suite and just move on – however this is the real world, not the ideal world, and I’m sure the path won’t be so smooth for everyone. Bear in mind though that you can initialise both extensions to use the same memcache storage, so its perfectly possible to migrate functionality across bit-by-bit.


This extension uses the improved functionality in the libmemcached library and, as such, a lot of the improvements are behind-the-scenes rather than something that users can clearly see. In particular there is much better support for working with multiple memcache servers, with grouping and so on – and all the connection-handling is done in libmemcached so there is much less for PHP to handle. There is also a raft of new features, so take a look at the manual for a full list but I’m going to run through a couple of my favourites in detail.

New Feature: getmulti

Allows the retrieval of multiple entries from the memcache server. This is really useful where you know you need a list of things and don’t want to have to make so many round trips. Its also pretty straightforward to do – consider this example, which extends the examples in the previous article, where we had a single user. This user now has a property called “friends” which contains an array of the ids of users they are friends with:

Rather than doing this repeatedly, we can send a single request over to the memcache server to give us all the data we need with the getmulti method.

The return value from this second function is identical to the return from the first version – the only difference is that we need only hit the memcache server a single time to get all the information rather than once per friend. As these data sets get larger, the benefit of techniques like this improve.

New Feature: cas (compare and swap)

This a neat function which performs the same action as replace – but it first checks whether the item has changed since it was last read by the current client. This protects against multiple updates coming in from different places. In order for this functionality to work, a system of tokens is provided. The tokens change every time an entry is written, and it is optional to retrieve this value when operating on an item. For example if the user editing their profile, and we want to be sure it didn’t change in the mean time, we might add a little check in:

This is fine so long as nothing else also updated the record stored at this key in the intervening period. For high-traffic sites, or situations where many users might be causing changes to the same data, this is a big issue. We can make a pseudo-atomic update by using the compare-and-swap functionality that the memcached functionality provides (health warning: This requires memcached 1.2.4 or later, some distros are older than this – for example Ubuntu Jaunty Jackalope only has 1.2.2 although the Karmic Koala version will be 1.2.8).

The compare-and-swap works on a token basis. When you retrieve the information from the cache, you also ask for a token. This token value changes every time the record is updated. When you write your next value back, you also supply the token from the get operation and the data is only stored when the token matches the current value – so you don’t unwittingly overwrite existing data.

That final $stored value will be false if the record has changed since we retrived it, at this point it might be appropriate to retrieve a new copy of the data and work with that or take some other evasive action, depending on the context of your application.

In Conclusion

The development of the functionality around memcache has been moving on quite a bit – and having an extension for PHP which allows us to work so easily with these features is a great thing. Although the extension is currently in beta, it is shaping up nicely and is well worth a look for anyone working with memcache or looking to begin to work with it. I think we’ll see this being adopted pretty quickly as even without the additional functionality, its performance is an improvement on the existing extension.