首页 » 编程笔记 » memcache » 正文

PHP扩展memcache与memcached对数据处理的区别


      最近在做memcache升级到memcached的过程中发现了一些区别,总结下
直接升级到memcached之后发现有些没有升级的程序会出现乱码情况,由于key用的都是一个,怀疑memcache 和memcached对数据的处理上可能有些区别,写测试用例发现:

<?php
$servers = array(
 array('127.0.0.1', 11211, 3)
);

$memcache = new Memcache;
$memcacheD = new Memcached;
$memcache->addServer('127.0.0.1',11211);
$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'));
    }
}

运行结果:

int型:
Checking WRITE with Memcache
Not compatible!array(2) {
["val"]=>
int(123)
["valD"]=>
string(3) “123″
}
Checking WRITE with MemcacheD
Not compatible!array(2) {
["val"]=>
bool(false)
["valD"]=>
int(123)
}

浮点型:
Checking WRITE with Memcache
Not compatible!array(2) {
["val"]=>
float(4542.32)
["valD"]=>
string(7) “4542.32″
}
Checking WRITE with MemcacheD
Not compatible!array(2) {
["val"]=>
bool(false)
["valD"]=>
float(4542.32)
}

简单字符串:
处理结果相同
Checking WRITE with Memcache
Checking WRITE with MemcacheD

布尔类型:
Checking WRITE with Memcache
Not compatible!array(2) {
["val"]=>
bool(true)
["valD"]=>
string(1) “1″
}
Checking WRITE with MemcacheD
Not compatible!array(2) {
["val"]=>
bool(false)
["valD"]=>
bool(true)
}

数组:
Checking WRITE with Memcache
Not compatible!array(2) {
["val"]=>
array(2) {
[0]=>
int(123)
[1]=>
string(6) “string”
}
["valD"]=>
int(0)
}
Checking WRITE with MemcacheD
Not compatible!array(2) {
["val"]=>
string(33) “a:2:{i:0;i:123;i:1;s:6:”string”;}”
["valD"]=>
array(2) {
[0]=>
int(123)
[1]=>
string(6) “string”
}
}

对象:
Checking WRITE with Memcache
Not compatible!array(2) {
["val"]=>
object(stdClass)#6 (1) {
["key1"]=>
string(6) “value1″
}
["valD"]=>
int(0)
}
Checking WRITE with MemcacheD
Not compatible!array(2) {
["val"]=>
string(43) “O:8:”stdClass”:1:{s:4:”key1″;s:6:”value1″;}”
["valD"]=>
object(stdClass)#6 (1) {
["key1"]=>
string(6) “value1″
}
}

结果发现,除了简单的字符串外,其他几种类型数据的处理都不太一样


在举例子说明
//通过memcached来set数据
$content = file_get_contents('http://www.putaor.com/');
$keyd = "footd";
$memcacheD->set($keyd, $content);

$val = $memcache->get($keyd);
$valD = $memcacheD->get($keyd);
if ($val !== $valD) {  
        print "Not compatible!";
        var_dump(compact('val', 'valD'));
}    

//通过memcache来set数据
$content = file_get_contents('http://www.putaor.com/');
$key = "foot";
$memcache->set($key, $content);
   
$val = $memcache->get($key);
$valD = $memcacheD->get($key);
if ($val !== $valD) {  
        print "Not compatible!";
        var_dump(compact('val', 'valD'));
}else{
        print "compatible";
         var_dump(compact('val', 'valD'));

}

运行结果发现:
通过memcached  set的数据,memcache扩展get回来之后是乱码,如截图:
通过memcache set的数据  两者取得数据相同

从以上测试结果总结来说,咱们还不能无缝的直接升级,解决办法:
1  memcached版本的需要统一升级memcache key的版本来解决
2 统一刷新memcache数据,不推荐