Unexpected unserialize call during memcache get, oryginal string expected

It took me a while to notice that one so i write it down for others.

Memcache extension for PHP has a set function that accepts options parameter. Please see details here.

I had strange problem that values were coming back from cache unserialized and i could not figure it out why. After some investigation it occurred to me that config setting of the application was wrong on the server and wrong parameter was passed to memcache set.

If you pass MEMCACHE_COMPRESSED == int(2) then values are compressed in cache. If you pass int(1) to set it will store it uncompressed and it will do something else to it so that when you retrieve it back you will get unserialized value instead of the oryginal one.

Here is little test code to see what happens

   $x     = array(1=>3); //array
   $xs    = serialize($x); // serialized array (string)
   $xss   = serialize($xs); // serialized string containing array (double serialization applied)
   $flag = 1; //2 compresses, 1 does something strange, 0 works perfectly
   
   $m->set('x',   $x, $flag);
   $m->set('xs',  $xs, $flag);
   $m->set('xss', $xss, $flag);
   
   var_dump( $m->get('x'));
   var_dump( $m->get('xs'));
   var_dump( $m->get('xss'));

Hope it helps someone.

Comments

Post new comment

Image CAPTCHA

About the author

Artur Ejsmont

Hi, my name is Artur Ejsmont,
welcome to my blog.

I am a passionate software engineer living in Sydney and working for Yahoo! Drop me a line or leave a comment.

Enjoy!