PHP Common caching way to get started tutorial
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 data
, introduced by the include method, and used.
Second, the required data is serialized through the Serialize function and saved directly to the file. When you need to use the cache
Data, read the contents of the file by deserializing it and copy it to the desired variable, and then use the.
Test results:
By testing, we found that the second way of serialize caching data is more efficient. (Data omitted,
Finally provide 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, this may be due to the time required to parse the array in the PHP file over the unserialize counter
The time the array was serialized. If you are interested you can view the PHP filesystem related functions and include
Research on the performance efficiency of 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.
Note:
After installing the PHP memory cache such as EA and APC, the first way to read cache through include is higher than
Two ways of serialize serialization caching. So in the moophp framework, we use the first
Two ways to cache: Sensitive information is cached in the first way