Foreword:
During the development of MooPHP, in order to find a more efficient caching method, the two most commonly used caching methods were tested.
Common caching methods in PHP:
The first is to process the data that needs to be cached and form a file that can be directly executed by PHP. 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, so you can test it yourself)
Reason analysis:
When reading the cache in include mode, PHP needs to perform several processes
1. Read the file
2. Parse the included files
3. Execution, when assigning a value to a variable
and reading the cache using serialize:
1.Read data
2. Deserialize data content
3. Assign values to variables
. From the above comparison, it may be that the time required to parse the array in the PHP file exceeds the time required to unserialize the array. If you are interested, you can check out "Research on Performance Efficiency 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 results will be updated here.
CacheTest_IncludeFile.php
CacheTest_SerializeFile.php
summary analysis:
The first, the advantage of include caching: increases 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, or even MySQL information. Second, the advantage of the serialize cache method is that it is faster.
Disadvantages: If the cache system file path is exposed, the cache content will be leaked.
Purpose: This method can be used when caching the latest articles, related articles, etc. when you don't worry about externally obtained data.
Remark:
When PHP memory caches such as ea and apc are installed, the first method of reading the cache through include will be faster than the second method of serializing the cache. Therefore, in the MooPHP framework, we cache non-sensitive information in the second way; sensitive information is cached in the first way. For an introduction to MooPHP, please refer to the article "Introduction to MooPHP Framework" (Address: http://www.ccvita.com/295.html )