Description
Performance has always been a point of Laravel framework, so tuning the Laravel program is a must-learn skill.
Next share some of the best practices for development, as well as tuning tips, and you are welcome to comment on other suggestions.
Here is a simple list:
- Configure Information caching
artisan config:cache
- Route cache
artisan route:cache
- Class-Map Loading optimizations
artisan optimize
- Automatic load optimization
composer dumpautoload
- Using Memcached to store sessions
config/session.php
- Using Professional cache Drives
config/cache.php
- Database Request Optimization
- Writing cache logic for datasets
- Use the instant compiler (JIT), such as: HHVM, Opcache
- Front-end Resource merging Elixir
1. Configure the information cache
Use the following Artisan to bring all the config
configuration information from the folder into one file, reducing the number of files loaded at runtime:
php artisan config:cache
The above command generates bootstrap/cache/config.php
a file, 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 bootstrap/cache/config.php
delete the file.
Note: The configuration information cache is not automatically overloaded with updates, so it is recommended that you turn off the configuration information cache during development, which is typically used in a production environment and can be used with the Envoy task runner.
2. Route caching
The routing cache can effectively improve the efficiency of the registration of routers, and in large applications The effect is more obvious, you can use the following command:
php artisan route:cache
The above command generates a bootstrap/cache/routes.php
file, and it is important to note that the routing cache does not support routing anonymous function authoring logic, as described in: Document-route caching.
You can use the following command to clear the route cache:
php artisan route:clear
The thing that this command does is to bootstrap/cache/routes.php
delete the file.
Note: Routing caches are not automatically overloaded with updates, so it is advisable to turn off the route cache during development, which is typically used in production environments and can be used with the Envoy task runner.
3. Class-Map Loading optimization
optimize
command to merge commonly loaded classes into a single file to improve operational efficiency by reducing file loading:
php artisan optimize --force
Will generate bootstrap/cache/compiled.php
and bootstrap/cache/services.json
two files.
You can config/compile.php
add the classes you want to merge by modifying the file.
In an environment where the production
parameters --force
do not need to be specified, the files are generated automatically.
To clear the class-map load optimization, run the following command:
php artisan clear-compiled
This command deletes optimize
the two files generated above.
Note: This command is to be run php artisan config:cache
after, because optimize
the command is generated based on the configuration information (e.g., config/app.php
providers
an array of files).
4. Automatic loading optimization
This command is not only for Laravel programs, but for all composer
programs that are used to build. This command PSR-0
PSR-4
converts and transforms a class mapping table to increase the load speed of a class.
composer dumpautoload -o
Note: php artisan optimize --force
This operation has already been done in the command.
5. Use Memcached to store sessions
Each Laravel request, will produce a session, modify the storage of the session can effectively improve program efficiency, session configuration information is config/session.php
proposed to modify the Memcached or Redis and other professional caching software:
=' memcached ',
6. Using Professional cache Drives
"Cache" is one of the magic weapon to improve the efficiency of the application, the default cache driver is the file
file cache, it is recommended to switch to a professional cache system, such as Redis or Memcached, not recommended for database caching.
‘default‘ => ‘redis‘,
7. Database Request Optimization
Database Request Optimization
- The data correlation model reads with lazy preload and preload;
- Use Laravel Debugbar or clockwork to pay attention to the total number of database requests per page;
This space is only relevant to Laravel, other information about data optimization, please consult other materials.
8. Writing cache logic for datasets
Reasonable use of Laravel provides cache layer operation, the data collection from the database to cache, reduce the pressure on the database, running in memory of professional cache software to read data is much faster than the database.
$posts=Cache::Remember(' Index.posts ',$minutes=30,function(){ReturnPost::With ( ' comments ' ' tags ' ' author ' ' seo ' ) ->wherehidden0->get (;}
remember
Even the data correlation model is cached, how convenient it is.
9. Using the Instant Compiler
HHVM and Opcache can easily let your application without any modification, directly improve the performance of 50% or higher, phphub before doing an experiment, see: Using Opcache to improve the performance of PHP 5.5+ program.
10. Front-End resource merging
As an optimization standard, a page should only load a CSS and a JS file, and the file to facilitate the CDN, need to change the filename with the changes.
Laravel Elixir offers a simple and practical solution, see documentation for details: Laravel Elixir documentation.
Laravel 5 Performance Optimization Tips