Getting started with common PHP cache methods-PHP source code

Source: Internet
Author: User
Ec (2); PHP common cache method entry tutorial first, process the data to be cached to form a file that PHP can directly execute. When data needs to be cached, it is introduced and used in the include mode. 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, copy the content to the required variable, and then use it. Test results: through the test, we found that the second type is the script ec (2) and script of serialize cache data.

Getting started with common PHP cache Methods
First, process the data to be cached to form a file that can be directly executed by PHP. When data needs to be cached

And use the include method.
Second, serialize the required data through the serialize function and save it directly to the file. When cache needs to be used

When reading data, read the file content through deserialization, copy it to the required variable, and then use it.

Test results:
Through tests, we found that the second method, namely serialize, caches data more efficiently. (Omitted data,

Finally, download the article address. You can test it on your own)

Cause analysis:
When reading the cache using the include method, PHP needs to execute several processes.
1. Read files
2. parse the included files
3. Execute and assign values to variables

When serialize is used to read the cache:
1. Read data
2. deserialize data content
3. assign values to variables

From the comparison of the above content, it may be because it takes more time to parse the array in the PHP file than the unserialize-

The time when the array is serialized. If you are interested, you can view the PHP filesystem related functions and include

Performance efficiency of require: http://www.ccvita.com/163.html

Test File Code:
: MooPHP-CacheTest.zip
Original address: http://www.ccvita.com/311.html has a new research experience will be updated here.
CacheTest_IncludeFile.php
CacheTest_SerializeFile.php

Summary:
First, the include cache Method
Advantage: It increases data confidentiality and security, and the cached content will not be discovered by the outside world.
Disadvantage: the speed is relatively slow.
Purpose: save data that is not known outside the system, such as web system settings and even MySQL information.
Second, serialize serialization cache Method
Advantage: fast.
Disadvantage: the file path of the cache system is exposed, and the cached content is exposed.
Purpose: This method can be used to cache the latest articles and related articles without worrying about external data.

Note:
When PHP memory caches such as ea and apc are installed, the first way to read the cache through include is faster

Two serialize serialization cache methods. Therefore, in the MooPHP framework, we use

Two caching methods: the first caching Method for sensitive information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.