Laravel 5 framework performance optimization skills, laravel Performance Optimization
Performance has always been a criticism of the Laravel framework, so tuning Laravel programs is a required skill.
Next, I will share some development best practices and optimization skills. If you have other suggestions, please leave a message to discuss them.
1. Configure Cache Information
Use the artisan Command provided by laravel to cache all configurations in the config file to a file.
php artisan config:cache
All cache information is stored in the bootstrap/cache/config. php file. You can also use the following command to delete the cache information:
php artisan config:clear
When the configuration information is modified, you can directly execute php artisan config: cache, because it will automatically execute php artisan config: clear first, and then execute php artisan config: cache.
2. Route information Cache
When there are more and more routes, especially in large projects, the routing cache effect is obvious.
php artisan route:cache
All the routes will be cached in the bootstrap/cache/routes. php file. If you want to use the route cache, then all the routes cannot use the closure, that is, the form of an anonymous function. You can also use
php artisan route:clear
Clear the route cache.
3. Class ing loading Optimization
The optimize command caches some common classes to files. By reducing file loading, the performance is improved:
php artisan optimize --force
The bootstrap/cache/packages. php and bootstrap/cache/services. php files are generated.
In the production environment, the parameter -- force does not need to be specified, and the file is automatically generated.
To clear the class ing loading optimization, run the following command:
php artisan clear-compiled
This command will delete the two files generated by optimize.
Note: This command runs after php artisan config: cache, because the optimize command is based on the configuration information (for example:config/app.php
Fileproviders
Array) to generate files.
4. Use redis/memcached to store session
Each laravel request generates session information. By default, the file storage method is used. It is stored in the storage/framework/sessions file and can be modified in config/session. php.
'driver' => 'redis',
5. Use a professional cache drive
"Cache" is a magic weapon to improve the efficiency of application programs. The default cache driver isfile
File caching. We recommend that you switch to a professional cache system, such as Redis or Memcached. We do not recommend that you use database caching.
You can modify the configuration file config/cache. php:
'default' => 'reids',