PHP program optimization
There are many contents in PHP program optimization. The following focuses on PHP code optimization, Session mechanism optimization, use of caching middleware and PHP code caching system (mainly APC acceleration).
and we have mastered enough mastered all the skills have been mastered to master enough skills, we can optimize the PHP code, we can optimize the PHP code. After the program coding work is completed, we usually ask some more senior programmers to review the code, evaluate the quality of the program and identify points that need optimization. This process is also called Code Review. Below we will introduce some common PHP programming techniques, as well as some optimization principles that may be involved in the Code Review process.
1. Upgrade to the latest PHP version
2. Reduce include and require
3. Use local variables instead of global variables
PHP has a very rich function library, and the same function can be completed using different functions. However, the operating efficiency of different functions is also different. We need to pay attention to it when using it. Below we compare some commonly used functions.
Although, for some programs whose logic is not very complex, the effect of each code optimization may not be very obvious, but It is very important to develop good programming habits, which is also the difference between ordinary programmers and advanced programmers. The above list is not all of the PHP programming skills, and mastering these skills cannot be completed overnight; the so-called learning has no limit, and only by constantly summarizing and accumulating in the process of learning and hands-on, can one's programming ability be improved to the next level. Floors.
PHP Optimized Session Mechanism
Simply put, Session is like a global variable that comes with each user, used to save any information that the user needs to save on the server. In fact, the functions of the Session session can be set in the system configuration file php.ini. Of course, we can also use the ini_set function (ini_get to obtain the configuration) to set it programmatically.
It is generally not recommended to enable auto_start (session.auto_start: whether to automatically enable it), because creating a Session requires consuming system resources. We usually only use the session_start function to enable the Session function when we need to use Session. Secondly, the validity period of Session needs to be determined according to the system conditions. If it is too long, it may cause load problems due to too much session data; if it is too short, it may also cause performance problems due to too frequent session creation. The system's default valid time is 1440 seconds, which is 24 minutes. In actual projects, we usually set this time between 1-8 hours. Also note that PHP The default storage method used by Session is file storage. In php.ini, we can select the required storage method through session.save_handle selection line. However, using file storage method is relatively inefficient and is not conducive to system architecture expansion. In actual projects, The session callback interface is often set through the session_set_save_handler method to control the logic of the session. Common storage media include databases, distributed cache servers, etc.
PHP Session optimization ideas. First of all, resource consumption will occur every time a Session is created. Do not use the session_start method in the global configuration file for granted. Secondly, you need to make sure to bring the Session ID with each session request, because if the server cannot obtain the Session ID, it will create a new one. In addition, when choosing a storage method, try to use fast storage media, such as cache servers Memcache(d), Redis, etc.
Use caching middleware
The emergence of caching middleware is to cache the queried information in the server memory to replace the database to handle most of the query requirements, thereby reducing the data load. pressure. Currently, the more commonly used caching middleware in the industry are Memcache and Redis (for the environment construction, usage, and differences between the two, readers please search for network resources, and I will not introduce them in detail here). Depending on the effect used in actual projects, caching middleware can usually greatly improve the query speed of the server. In addition, the Redis cache can also be used as a write queue, that is, the data is first written into the Redis cache and then transferred to the data.
Use APC to accelerate
With the continuous development of network applications, logic code has become more and more complex, and the resource consumption of introducing huge class library code into the framework is also relatively high. So when you go online again. We also need to use some code-level caching to speed up code execution.
APC (Alternative PHP Cache, PHP code caching system) is a very good PHP code caching solution. It improves PHP execution efficiency by caching and optimizing PHP intermediate code (opcode).
Note: Free PHP code caching technologies at the same level as APC include eAccelerator and XCache (installation and differences: http://blog.csdn.net/mossader/article/details/6343354)
Optimize data transmission
2. Use gzip compression
To be continued...
Note: This article Excerpted from Chapter 9: Server Side of "Best Practices for Android and PHP Development" Optimized (with deletions)
The above has introduced PHP program optimization, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.