Laravel Performance Optimization

Source: Internet
Author: User

1. Configure the information cache

Use the following Artisan to bring all the configuration information in the Config folder into one file, reducing the number of files loaded at runtime:

  

PHP Artisan Config:cache

The above command generates the 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 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 the bootstrap/cache/routes.php file, and it is important to note that the routing cache does not support routing anonymous function authoring logic.

You can use the following command to clear the route cache:

PHP Artisan Route:clear

The thing that this command does is to delete the bootstrap/cache/routes.php 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

The optimize command merges commonly loaded classes into a single file, improving operational efficiency by reducing file loading:

PHP Artisan optimize--force

Generates bootstrap/cache/compiled.php and Bootstrap/cache/services.json two files.

You can add classes to be merged by modifying the config/compile.php file.

In the production environment, the parameter--force does not need to be specified, and the file is generated automatically.

to clear the class-map load optimization, run the following command:  

PHP Artisan clear-compiled

This command deletes the two files generated by the optimize above.

Note: This command runs after PHP artisan config:cache because the optimize command generates files based on configuration information such as providers array of config/app.php files.

4. Automatic loading optimization

This command is not only for Laravel programs, but for all programs that are built using composer. This command converts PSR-0 and PSR-4 to a class-map table to increase the load speed of the class.

Composer Dumpautoload-o

Note: This operation has already been done in PHP artisan optimize--force 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, recommended to modify to Memcached or Redis and other professional caching software:

' Driver ' = ' 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 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

  

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 = +, function ()

{

return Post::with (' comments ', ' tags ', ' author ', ' SEO ')->wherehidden (0)->get ();

});

Remember even the data correlation model has been 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 Performance Optimization

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.