PHP Array Cache: Three ways to compare JSON, serialization, and Var_export

Source: Internet
Author: User

Using PHP's site system, you have to introduce a caching mechanism in the face of large data volumes. One simple and effective way is to cache the PHP object into a file. Let me explain and compare these 3 kinds of caching methods.

The first method: JSON
The JSON cache variable is mainly used Json_encode and Json_decode two PHP functions. Json_encode can change the variable to a text format so that it can be stored in a file.
Use the following example:

    1. Store Cache
    2. File_put_contents($cachePath, json_encode($myDataArray));
    3. Retrieve Cache
    4. $myDataArray = json_decode(file_get_contents($cachePath));

Advantage:

    • Still readable after serialization of variables
    • Can be used for other systems because the JSON format is standard

Disadvantage:

    • Only valid for UFT-8 data, other encodings may not work well
    • Valid only for examples of stdclass classes


The second method: serialization
The way to serialize is mainly using the 2 functions of serialize and Unserialize, both serialized and JSON, are stored as text.
Using the example

    1. Store cache
    2. File_put_contents($cachePath, serialize($myDataArray));
    3. Retrieve cache
    4. $myDataArray = unserialize(file_get_contents($cachePath));

Advantage:

    • Allow non-UTF-8 variables
    • Support for other instances except for Stdclass exceptions

Disadvantage:

    • The encoded text is unreadable to people.
    • Cannot be referenced by a system in another language

Method Three: Var_export
This method uses the Var_export function to print the contents of the variable into a PHP file, using the Include method to retrieve the contents of the variable. So the generated cache file when a php file, the content is as follows

    1. <? PHP
    2. return/ *var_export output */;
    3. ?>

Examples of Use:

    1. //Store cache
    2. file_put_contents ( $cachePath "<?php\nreturn" . ( $myDataArray , true) . ";"
    3. //Retrieve cache
    4. $myDataArray = Include ( $cachePath

Advantage:

    • No requirement for encoding format, allows non-UTF-8 encoding
    • Easy to read cache files
    • When you get a variable, you use the language attribute directly, not the function
    • When using opcode, the cached php file is placed in the opcode cache (which is actually a disadvantage)

Disadvantage:

    • Objects without the __set_state method cannot be cached
    • Var_export out of the variable can not have the effect of PHP parsing content, triggering syntax errors, may affect your PHP application


Performance testing
The following tests are performed using an array of 5 different sizes (904B, ~18kb, ~250kb, ~4.5MB, and ~72.5MB).

    1. Encode data 10 times using an encoding function
    2. Calculate the size of the encoded data
    3. Decoding the encoded data 10 times

Conclusion
2.53GHz, 4GB, Ubuntu Linux, PHP 5.3.0RC4. On this configured notebook, the test results are as follows:



The above 2 graphs indicate that 100% is the best, and the Var_export is poor in encoding and decoding performance. It is recommended to use the serialization method when the data volume is small, if the amount of data is very large, it is necessary to consider the data structure problem.

http://www.opstool.com/article/262

PHP Array Cache: Three ways to compare JSON, serialization, and Var_export

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.