0

I am trying to share my memcache server with my rails and php server.

Rails:

my_var = {'one'=>1,'two'=>2}
Rails.cache.write 'hello', PHP.serialize(my_var), :raw => true
Rails.cache.read 'hello'

output:

"a:2:{s:3:\"one\";i:1;s:3:\"two\";i:2;}"

PHP:

$var = self::$memcache->get('hello');
die(var_dump($var));

output:

"a:2:{s:3:\"one\";i:1;s:3:\"two\";i:2;}"

PHP.serialize is a function from gem php_serialize. I was hoping that my PHP server can pickup hello and produces an array. Could anyone please help me which part that I do wrong here?

Thank you

4

2 回答 2

1

Memcached gives back the serialized (marshaled) string. To use the actual array, you need to unserialize it in PHP first, just like you have to serialize the array in Ruby.

Try

$var = unserialize(self::$memcache->get('hello'));
die(var_dump($var));
于 2012-05-29T11:17:05.847 回答
0

PHP 和这里的 ruby​​ 的序列化意味着您将对象转换为文本表示形式,即平面字符串。在 PHP 中,这将通过函数serialize来完成。要真正拥有传递对象的新实例,您需要从这个文本表示中重新创建它,您需要 PHP 中的另一个函数unserialize

所以在你的情况下,就像@chiborg 说的那样,使用 unserialize 会得到你的数组

var_dump(unserialize("a:2:{s:3:\"one\";i:1;s:3:\"two\";i:2;}"));

array(2) {
  ["one"]=>
  int(1)
  ["two"]=>
  int(2)
}
于 2012-05-29T11:26:13.137 回答