Performance has always been a point of laravel framework, so tuning Laravel program is a must-learn skill. Next, share some of the best practices of development, as well as tuning techniques, and we have other suggestions and comments to discuss.
Performance has always been a point of laravel framework, so tuning Laravel program is a must-learn skill.
Next, share some of the best practices of development, as well as tuning techniques, and we have other suggestions and comments to discuss.
Here is a simple list:
Configure Information Caching artisan Config:cache
Routing Cache Artisan Route:cache
Class mapping load Optimization artisan optimize
Automatic load Optimization composer Dumpautoload
Use Memcached to store sessions config/session.php
Using a professional cache drive config/cache.php
Database Request Optimization
Writing cache logic for datasets
Use the Just-in-time compiler (JIT), such as: HHVM, Opcache
Front-End resource Merge Elixir
1. Configuration Information Caching
Use the following Artisan to merge all the configuration information from the Config folder into a single file, reducing the number of runtime files loaded:
PHP Artisan Config:cache
The above command generates file bootstrap/cache/config.php, and you can use the following command to cancel the configuration information cache:
PHP Artisan Config:clear
The thing that this command does is to delete the bootstrap/cache/config.php file.
Note: The configuration information cache is not automatically overloaded with updates, so it is recommended that you turn off the configuration information cache, which is typically used in production environments, and can be used in conjunction with the envoy Task Manager.
2. Routing Caching
Routing caching can effectively improve the registration efficiency of routers, and the effect is more obvious in large applications, you can use the following command:
PHP Artisan Route:cache
The above command generates the bootstrap/cache/routes.php file, and it should be noted that routing caching does not support routing anonymous function writing logic.
You can use the following command to clear the routing cache:
PHP Artisan Route:clear
The thing that this command does is to delete the bootstrap/cache/routes.php file.
Note: Routing caching is not automatically overloaded with updates, so it is recommended that you turn off routing caching, which is typically used in production environments, and can be used in conjunction with the envoy Task Manager.
3. Class Mapping Load Optimization
The optimize command merges the commonly loaded classes into a single file to improve operational efficiency by reducing file loading:
PHP Artisan optimize--force
bootstrap/cache/compiled.php and Bootstrap/cache/services.json two files are generated.
You can add classes to be merged by modifying the config/compile.php file.
In a production environment, the parameter--force is not required to be specified and the file is automatically generated.
To clear class mapping load optimization, run the following command:
PHP Artisan clear-compiled
This command deletes the two files generated above optimize.
Note: This command runs after PHP artisan config:cache because the optimize command generates files based on configuration information, such as the providers array of config/app.php files.
4. Automatic load optimization
This command is not only for laravel programs, but for all programs built using composer. This command converts PSR-0 and PSR-4 into a class mapping table to increase the load speed of the class.
Composer Dumpautoload-o
Note: This operation has been done in the PHP artisan optimize--force command.
5. Use Memcached to store sessions
Each laravel request, will produce a session, modify the session storage mode can effectively improve the efficiency of the program, the session configuration information is config/session.php, proposed to modify the Memcached or Redis and other professional caching software:
' Driver ' => ' memcached ',
6. Using a professional cache drive
Caching is one of the keys to improving the efficiency of your application, the default cache driver is the file cache, and it is recommended that you switch to a professional caching system, such as Redis or Memcached, which is not recommended for use with database caching.
' Default ' => ' Redis ',
7. Database Request Optimization
The Data association model is read with deferred preload and preload;
Use Laravel Debugbar or clockwork to pay attention to the total number of database requests per page;
This space is only written to laravel related, other data optimization content, please check other information.
8. Write Cache logic for datasets
Reasonable use of laravel provided by the cache layer operation, from the database to take out the collection of data to cache, reduce the pressure of the database, running on the memory of the professional cache software to read the data much faster than the database.
$posts = Cache::remember (' index.posts ', $minutes =, function ()
{
return Post::with (' comments ', ' tags ', ' author ', ' SEO ')->wherehidden (0)->get ();
});
Remember even the data association model is also cached, how convenient ah.
9. Using the Just-in-time compiler
HHVM and Opcache are easy to let your application without any modification of the situation, directly improve the performance of 50% or higher, phphub before doing an experiment, see: Use Opcache to improve PHP 5.5+ program performance.
10. Front-End Resource consolidation
As the standard of optimization, a page should only load a CSS and a JS file, and the file to be able to easily go CDN, the need for file name changes with the change.