instructions

Performance has always been a problem with Laravel frameworks, so tuning Laravel applications is a must-learn skill.

Let’s share some development best practices, tuning tips, and other suggestions in the comments.

Here’s a simple list:

  1. Configuring Information Cachingartisan config:cache
  2. The routing cacheartisan route:cache
  3. Class map loading optimizationartisan optimize
  4. Auto-loading optimizationcomposer dumpautoload
  5. Use Memcached to store sessionsconfig/session.php
  6. Use a professional cache driveconfig/cache.php
  7. Database request optimization
  8. Write cache logic for data sets
  9. Use just-in-time compilers (JIT), such as HHVM and OpCache
  10. Front-end resources merged with Elixir

1. Configure information caching

Use the following Artisan commands to consolidate all configuration information from the config folder into one file to reduce the number of files loaded at runtime:

php artisan config:cacheCopy the code

The above command will generate the bootstrap file/cache/config. PHP, you can use the following command to cancel the cache configuration information:

php artisan config:clearCopy the code

This command to do is to put the bootstrap/cache/config. The PHP file deletion.

Note: The configuration cache does not automatically reload with updates, so it is recommended to turn off the configuration cache at development time. It is commonly used in production environments and can be used in conjunction with the Envoy task runner.

2. Route cache

Route cache can effectively improve the registration efficiency of routers, which is more obvious in large applications. You can run the following command:

php artisan route:cacheCopy the code

The above command will generate the bootstrap/cache/routes. The PHP file, it is important to note that the routing cache does not support routing anonymous functions to write logic, see: document – routing cache.

You can clear the route cache with the following command:

php artisan route:clearCopy the code

This command to do is to put the bootstrap/cache/routes. The PHP file deletion.

Note: The route cache does not automatically reload with updates, so it is advisable to turn off the route cache during development. It is commonly used in production environments and can be used in conjunction with the Envoy task runner.

3. Class mapping loading optimization

The optimize command combines commonly loaded classes into a file to improve efficiency by reducing file loads:

php artisan optimize --forceCopy the code

Generates the bootstrap/cache/compiled. PHP and bootstrap/cache/services. The json two files.

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

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

To clear class map load optimizations, run the following command:

php artisan clear-compiledCopy the code

This command deletes the two files generated by optimize above.

Note: This command is run from PHP artisan config:cache, because the optimize command generates files based on configuration information such as the providers array of the config/app.php file.

4. Auto-load optimization

This command applies not only to Laravel programs, but to all programs built using Composer. This command converts the PSR-0 and PSR-4 into a class mapping table to speed up class loading.

composer dumpautoload -oCopy the code

Note: this is already done in the PHP artisan optimize –force command.

5. Use Memcached to store the session

Each Laravel request generates a session. Changing the storage mode of the session can effectively improve the efficiency of the program. The configuration information of the session is config/session. PHP.

'driver' => 'memcached',Copy the code

6. Use a professional cache drive

Cache is one of the magic weapons to improve the running efficiency of applications. The default cache driver is file cache. You are advised to switch to a professional cache system, such as Redis or Memcached, instead of using database cache.

'default' => 'redis',Copy the code

7. Database request optimization

Database request optimization

  • Delay preloading and preloading are used when data association model is read.
  • Use Laravel Debugbar or Clockwork to watch the total number of database requests per page;

The space here is only for Laravel related, other data optimization content, please refer to other materials.

8. Write cache logic for the dataset

Rationally use the cache layer operation provided by Laravel to cache the data set taken out from the database and reduce the pressure of the database. The professional cache software running in memory can read the data much faster than the database.

$posts = Cache::remember('index.posts', $minutes = 30, function()
{
    return Post::with('comments', 'tags', 'author', 'seo')->whereHidden(0)->get();
});Copy the code

Remember even caches the data association model as well, how convenient.

9. Use a just-in-time compiler

Both HHVM and OpCache can easily improve the performance of your application by 50% or more without making any changes. PHPhub has done an experiment to see how OpCache improves PHP 5.5+ Application performance.

10. Merge front-end resources

As a standard of optimization, only one CSS and one JS file should be loaded on a page, and the file name should change with the modification if the file can be easily CDN.

Laravel Elixir offers a simple and practical solution, detailed in the Laravel Elixir documentation.

@ Summer | Work at: optimal sail, we are looking for a full-time partner and Remote engineer, welcome to join, bowtie: