Three methods of File Cache data in PHP

Source: Internet
Author: User

The so-called File Cache data is to put some non-frequently changed data in the file for use, instead of getting data through database Io.

1. var_export ($ array, [True])

#. Uchome and discuz systems use a relatively large number of methods, usually to store some system configs data such as credit rules, mail configuration information, etc., by the method cache_write () store the data read from the database to a file.

Eg:

Var_export ($ ARR); // directly outputs a string

$ A = var_export ($ arr, true); // assign this string to a variable

Echo $ A; // same as var_export ($ ARR );

Benefit: var_export () saves an array as an array like "array ('A' => array (....), the string 'B' => XXX) "completely retains the array format, so you only need to splice the string: '$ arr_name = '. var_export ($ ARR); in this way, it is easy to call the data. You only need to input the file require to use this array $ arr_name.

2. serialize ()/unserialize ()

Eg:

$ Se = serialize ($ ARR); // return string

$ Unse = unserialize ($ SE); // return Array

3. json_encode/json_decode

#. Here we need to figure out the relationship between the JSON object and the associated array.

Eg:

$ Json_en = json_encode ($ ARR); // return string

$ Json_de1 = json_decode ($ json_en); // return JSON object

Echo $ json_de1-> attr1; // outputs attributes as a JSON object

$ Json_de2 = json_decode ($ json_en, true); // force return Array

Echo $ json_de2 ['attr1']; // output as an associated array

[Test] serialize/unserialize is the fastest among the three types.

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.