note 115666 added to book.memcache
| From: | gabriel dot maybrun at demandmedia dot com | Date: | Thu, 04 Sep 2014 23:58:44 +0000 |
| Subject: | note 115666 added to book.memcache | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200370@lists.php.net to get a copy of this message | ||
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'));
}
}
----
Server IP: 216.194.115.244
Probable Submitter: 208.46.190.178
----
Manual Page -- http://php.net/manual/en/book.memcache.php
Edit -- https://master.php.net/note/edit/115666
Del: integrated -- https://master.php.net/note/delete/115666/integrated
Del: useless -- https://master.php.net/note/delete/115666/useless
Del: bad code -- https://master.php.net/note/delete/115666/bad+code
Del: spam -- https://master.php.net/note/delete/115666/spam
Del: non-english -- https://master.php.net/note/delete/115666/non-english
Del: in docs -- https://master.php.net/note/delete/115666/in+docs
Del: other reasons-- https://master.php.net/note/delete/115666
Reject -- https://master.php.net/note/reject/115666
Search -- https://master.php.net/manage/user-notes.php