PHP Common Caching Method:
The first type of data that needs to be cached is processed to form files that PHP can execute directly. When you need to cache the data, introduce it by using the Include method and use it.
Second, the required data is serialized through the Serialize function and saved directly to the file. When you need to use cached data, read the contents of the file and copy it to the desired variable by deserializing it, and then use the.
Test results:
By testing, we found that the second way of serialize caching data is more efficient. (data omitted, finally provided the article address download, you can Test yourself)
Reason Analysis:
Include way to read the cache, PHP needs to perform several procedures
1. Read the file
2. Resolve the files contained in
3. Execution, assigning values to variables
While serialize reads the cache in a serialized way:
1. Reading data
2. Deserialization of data content
3. Assigning values to variables
In contrast, it is possible that parsing an array in a PHP file takes more time than unserialize deserializing the array. If you are interested you can view the performance efficiency study of PHP filesystem related functions and include require: http://www.ccvita.com/163.html
Test file Code:
Download Address: Moophp-cachetest.zip
Original address: http://www.ccvita.com/311.html New research experience will be updated here.
cachetest_includefile.php
cachetest_serializefile.php
Summary and Analysis:
First, the way include caching
Advantages: Increase the confidentiality of data, and security, cache content will not be found outside.
Disadvantage: The speed is relatively slow.
Use: Save data that is not known outside the system, such as Web system settings, and even MySQL information.
Second, serialize the way the cache is serialized
Advantages: Faster.
Disadvantage: Cache system file path a little exposure, cache content will be leaked.
Use: Cache the latest articles, related articles, etc. do not worry about external data, you can use this method.