Memcached Redux

I’ve been reading about Couchbase, and I knew it was compatible with Memcached out of the box. One of the features I wanted to start using was Memcached::getMulti and Memcached::setMulti. I knew the Memcached WP Object Cache plugin had a get_multi method, but I didn’t know what it did or how it did it. Turns out, it doesn’t implement Memcached::getMulti.

When I looked under the hood, I realized that the Memcache extension is loads different than the Memcached extension in PHP. Memcached has the getMulti method, Memcache does not. So I set out to change this: I have altered the famed Memcached plugin to actually use the Memcached class.

Because I did this, you can now use methods like this:

wp_cache_get_multi( array(
	array( 'key', 'group' ),
	array( 'key', '' ),
	array( 'key', 'group' ),
) );

wp_cache_set_multi( array(
	array( 'key', 'data', 'group' ),
	array( 'key', 'data' )
) );

Rather than making many calls to grab data, especially related data, you can grab it in one hop.

I’m gonna keep working on the plugin, and drop a line if you install it and have comments / concerns.

The Plugin: Memcached Redux

17 thoughts on “Memcached Redux

  1. In your plugin following properties are initialized arrays:

    var $global_groups = array();

    var $no_mc_groups = array();

    Do you need to assign a value to the properties like so?:

    var $global_groups = array (‘users’, ‘userlogins’, ‘usermeta’, ‘site-options’, ‘site-lookup’, ‘blog-lookup’, ‘blog-details’, ‘rss’);

    var $no_mc_groups = array( ‘comment’, ‘counts’ );

    var $autoload_groups = array (‘options’);

    Is this plugin working for you without leakage or data loss? Are you testing it on a production server?

    • I rolled a change a few weeks ago – memcached on Linux was returning -1 instead of NULL for empty / non-existent values, I was only checking for === NULL. Maybe that was it? Try it again.

  2. Pingback: Working With Memcached in WordPress | 10up

  3. Gave a try. It makes the blog inaccessible. Doesn’t work at all.

    I currently use version 2.0.1 which leaks and loses a lot of data, and only runs about 84% efficiency, as opposed to 2.0 which ran at about 95% efficiency or better. I can’t use 2.0 any longer because I now have a mixed environment of single site and multisite installations.

    Box is CentOS 6.2 running the latest Memcached from YUM repository.

    Are you using your plugin?

  4. I just figured out the problem. Your plugin requires the memcached extension, which is the reason you can use the PHP multi functions:

    The Memcached 2.0.1 plugin requires the old memcache extension, which has no PHP multi functions:

    I would have to switch everything over to the new extension to try your plugin, which I will one late night to see how well it does.

  5. Just wanted to correct this statement from above when I said:

    “I currently use version 2.0.1 which leaks and loses a lot of data, and only runs about 84% efficiency, as opposed to 2.0 which ran at about 95% efficiency or better.”

    I discovered the problem was WordPress SEO plugin written by Yoast. It was executing the wp_cache_flush, and wp_cache_delete functions with extreme frequency. After commenting out the two lines with those functions all my problems went away, and now the GET hits are back to 95% or better.

    Didn’t want anyone getting a wrong impression about the Memcached Object Cache 2.0.1 plugin.

  6. Are you sure the Memcache module doesn’t support multi-gets? The get function accepts an array of keys and will return an array of values. I’m not sure how this is implemented under the hood, but it may get multiple keys at once.

Comments are closed.