Home >Backend Development >PHP Tutorial >redis saves the array (json_encode is unsuccessful, serialize is successful)

redis saves the array (json_encode is unsuccessful, serialize is successful)

WBOY
WBOYOriginal
2016-10-10 11:56:242108browse

Use redis to store arrays and store them in string type.
Use PHP's own json_encode and json_decode to convert to json (failed).
PHP’s own serialization functions serialize and unserialize functions (successful).
Are there any specific differences? Can anyone tell me the principles of success and failure?

Reply content:

Use redis to store arrays and store them in string type.

Use PHP's own json_encode and json_decode to convert to json (failed).
PHP’s own serialization functions serialize and unserialize functions (successful).
Are there any specific differences? Can anyone tell me the principles of success and failure?

It doesn’t seem to have much to do with redis. The JSON strings of set and get should be consistent.

You can directly print the string of

json_encode

and the result of json_decode to see. I guess it is possible that

json_decode($str, true)

has missing parameters and is written as json_decode($str), causing the result to be an object instead of an array.

Serialization and JSON are not essentially the same thing, json is a data format

Serialization refers to converting data objects in the running environment into stream data so that it can be saved to a file or transmitted over the network for use elsewhere.

After deserialization, this stream data will be restored to a data object in the language

Every language has its own serialization method

php has a more efficient method than serialize, which is

igbinary_serialize() and

igbinary_unserialize()
These two methods need to install the ibginary module
Configuration when using redis

<code>$reids->setOption(Redis::OPT_SERIALIZER,Redis::SERIALIZER_IGBINARY);
</code>

Redis will automatically serialize, instead of serializing and then setting every time, deserializing after every get will be done

About serialization, take PHP as an example

<code>class Person{
    private $_name = "default";
    public function get_name(){
        return $this->_name;
    }
}

$p = new Person;
$sp = serialize($p);

send2serverB($p);</code>

You serialized an object instance in server A, and you need to use the instance in service B

Of course, server B also needs to have a declaration of class Person in order to deserialize it normally

<code>function recieve_handle($sp){
   $p = unserialize($p);
   // 反序列后, 能还原Person实例, 能使用实例的方法
   // json只是种保存元数据的格式, 无法保存对象
   $name = $p->get_name();
}</code>

Because data needs to be saved or transmitted (so-called I/O), so there is serialization. Json is a commonly used data stream format, but it can only store metadata and cannot express complex object properties and methods.


Note that the data must be utf8,

gbk cannot be encoded


It’s just that json_encode cannot serialize objects

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn