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:
- Store Cache
- File_put_contents($cachePath, json_encode($myDataArray));
- Retrieve Cache
- $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
- Store cache
- File_put_contents($cachePath, serialize($myDataArray));
- Retrieve cache
- $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
- <? PHP
- return/ *var_export output */;
- ?>
Examples of Use:
- //Store cache
- file_put_contents ( $cachePath "<?php\nreturn" . ( $myDataArray , true) . ";"
- //Retrieve cache
- $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).
- Encode data 10 times using an encoding function
- Calculate the size of the encoded data
- 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