Home >Backend Development >PHP Tutorial >Performance Testing of PHP File Cache_PHP Tutorial
Common caching methods for PHP :
The first is to process the data that needs to be cached and form a file that PHP can directly execute. When you need to cache data, introduce it through include and use it.
Second, serialize the required data through the serialize function and save it directly to the file. When you need to use cached data, read the file content through deserialization and copy it to the required variables, and then use it.
Test results:
Through testing we found that the second way of caching data via serialize is more efficient. (The data is omitted, and the article address is provided for download at the end. You can test it yourself)
Cause analysis:
When reading the cache in include mode, PHP needs to perform several processes
1. Read the file
2. Parse the included file
3. Execute and assign values to variables
When reading the cache in serialize mode:
1. Read data
2. Deserialize data content
3.Assign values to variables
Summary analysis:
The first, include caching method
Advantages: Increase the confidentiality and security of data, and the cached content will not be discovered by the outside world.
Disadvantages: Relatively slow.
Purpose: Save data that is prohibited from being known outside the system, such as web system settings and even MySQL information
Second, serialize cache method
Advantages: faster.
Disadvantage: Once the cache system file path is exposed, the cache content will be leaked.
Purpose: You can use this method when caching the latest articles, related articles, etc., when you don’t worry about externally obtained data.