Laravel 5 Performance Optimization Tips

Source: Internet
Author: User
Tags hhvm sessions

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:

    1. Configure Information cachingartisan config:cache
    2. Route cacheartisan route:cache
    3. Class-Map Loading optimizationsartisan optimize
    4. Automatic load optimizationcomposer dumpautoload
    5. Using Memcached to store sessionsconfig/session.php
    6. Using Professional cache Drivesconfig/cache.php
    7. Database Request Optimization
    8. Writing cache logic for datasets
    9. Use the instant compiler (JIT), such as: HHVM, Opcache
    10. 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

optimizecommand 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 (;}         

rememberEven 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

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.