Memcached

User Contributed Notes

gabriel dot maybrun at demandmedia dot com 04-Sep-2014 11:59
GOTCHA: Recently I was tasked with moving from PECL memcache to PECL memcached and ran into a major problem -- memcache and memcached serialize data differently, meaning that data written with one library can't necessarily be read with the other library.

For example, If you write an object or an array with memcache, it's interpreted as an integer by memcached.  If you write it with memcached, it's interpreted as a string by memcache.

tl;dr - You can't safely switch between memcache and memcached without a either a cache flush or isolated cache environments.

<?php
$memcache
= new Memcache;
$memcacheD = new Memcached;
$memcache->addServer($host);
$memcacheD->addServers($servers);

$checks = array(
   
123,
   
4542.32,
   
'a string',
   
true,
    array(
123, 'string'),
    (object)array(
'key1' => 'value1'),
);
foreach (
$checks as $i => $value) {
    print
"Checking WRITE with Memcache\n";
   
$key = 'cachetest' . $i;
   
$memcache->set($key, $value);
   
usleep(100);
   
$val = $memcache->get($key);
   
$valD = $memcacheD->get($key);
    if (
$val !== $valD) {
        print
"Not compatible!";
       
var_dump(compact('val', 'valD'));
    }

    print
"Checking WRITE with MemcacheD\n";
   
$key = 'cachetest' . $i;
   
$memcacheD->set($key, $value);
   
usleep(100);
   
$val = $memcache->get($key);
   
$valD = $memcacheD->get($key);
    if (
$val !== $valD) {
        print
"Not compatible!";
       
var_dump(compact('val', 'valD'));
    }
}
davidt 03-Jan-2013 01:14
The module also supports SASL authentication, it just isn't documented sadly. You'll need to run the following code:

<?php
$m
= new Memcached();
$m->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$m->setSaslAuthData("user-1", "pass");
?>

You need to enable the "memcached.use_sasl = 1" ini option for memcached in the php.ini file.
mike at eastghost dot com 06-Feb-2012 04:05
It's Feb 2012, and I just spent half a day tracing a weird and sporadic problem back to Memcached extension (vers 1.0.2), which would randomly return SYSTEM ERROR and fail to either get or put data into unix memcached.  Made one single change from Memcached extension back to Memcache extension and problem instantly vanished.

A year or three ago, I had changed from Memcache to Memcached for some other reason.

Now I'm on a mission to transcend the Memcache/Memcached fiasco.  I'm going to try APC's variable cache, and if that doesn't work better than just revert to using a key:value stash collection in Mongo.

Basically, the Memcache / Memcache driver fiasco, which has been going on apparently for years now, has ruined serious use of unix memcached on PHP...unless you're huge and can afford to create your own MemcacheX driver...
joelhy 22-May-2011 08:27
For those confuse about the memcached extension and the memcache extension, the short story is that both of them are clients of memcached server, and the memcached extension offer more features than the memcache extension.