Didzis
Didzis

Reputation: 453

PHP Memcached stores array, retrieves object. A bug?

Using MemcacheD v1.4.22

PECL MemcacheD library v2.2.0

PHP v5.3.19 (cli)

I am trying to store an array into cache

$array = $sql->get_arr ('SELECT properties FROM obj_properties WHERE obj_id="'.$id.'"');

var_dump($array); // output below

array(10) {
  [0]           => string(3) "976"
  ["parent"]    => string(3) "976"
  [1]           => string(5) "Ziedi"
  ["name"]      => string(5) "Ziedi"
  [2]           => string(0) ""
  ["int_name"]  => string(0) ""
  [3]           => string(1) "1"
  ["type"]      => string(1) "1"
  [4]           => string(1) "1"
  ["status"]    => string(1) "1"
}

$memcached->set( $memKey, $ret_val, $max_time );

$returnArray = $memcached->get( $memKey );

var_dump($returnArray); // output below

object(stdClass)#5 (10) {
  ["0"]         => string(3) "976"
  ["parent"]    => string(3) "976"
  ["1"]         => string(5) "Ziedi"
  ["name"]      => string(5) "Ziedi"
  ["2"]         => string(0) ""
  ["int_name"]  => string(0) ""
  ["3"]         => string(1) "1"
  ["type"]      => string(1) "1"
  ["4"]         => string(1) "1"
  ["status"]    => string(1) "1"
}

I am storing an array with mixed integer/string keys which should not be a problem for php but apparently it is for memcached? Is it documented anywhere? Any workaround or hint?

If I look at terminal output of the key:

$ telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

get dev_id_1_04115ec378e476c56d19d827bcf8db56
VALUE dev_id_1_04115ec378e476c56d19d827bcf8db56 6 114
{"0":"976","parent":"976","1":"Ziedi","name":"Ziedi","2":"","int_name":"","3":"1","type":"1","4":"1","status":"1"}

we see that the array is actually saved as object and not as array meaning the output is correct but somehow storing converts this array into object...

Unfortunately I cannot get rid of string or int based array key elements so I will have to deal with them somehow.

Any help appreciated.

Upvotes: 1

Views: 1313

Answers (1)

Didzis
Didzis

Reputation: 453

After two days of painful research it turns out that memcached configuration was set to serialize to objects in php.ini file:

memcached.serializer = "json"

changed it to

memcached.serializer = "json_array"

As always, thanks Stackoverflow - even if it does not give you the answer, it makes you think more constructive.

Upvotes: 5

Related Questions