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: Configuration information cache Artisan Config: Cache route cache Artisan Route: Cache class mapping optimization Artisan Optimize Automatic loading optimization composer Dumpautoload Use Memcached To store session config/session.php use professional cache drive config/cache.php database requests optimized for data set writing cache logic using just-in-time compiler (JIT) such as: HHVM, OpCache front-end resource merge Elixir
1. Configuration information cache Use the following Artisan command to consolidate all the configuration information in the config folder into one file to reduce the number of files loaded at runtime: PHP artisan config: cache 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: clear 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 The route cache can effectively improve the registration efficiency of routers and is more effective in large applications. You can run the following command: PHP artisan route: cache 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. Use the following command clears the routing cache: PHP artisan route: the clear 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.
The optimize command combines commonly loaded classes into a file to improve efficiency by reducing file loads: PHP artisan optimize — force 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 the class map load optimizations, run the following command: PHP artisan clear-compiled 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 is not only for Laravel programs, but also for 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 -o Note: the PHP artisan optimize –force command already does this.
Each Laravel request generates a session. Modify the session storage mode to improve the efficiency of the program. The session configuration information is config/session.php. You are advised to use professional cache software such as Memcached or Redis. ‘driver’ => ‘Memcached’.
The default cache driver is file cache. You are advised to use a professional cache system, such as Redis or Memcached. Database cache is not recommended. ‘default’ => ‘redis’,
7. Use delayed preloading and preloading when the database requests to optimize data association model reading; 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 data set reasonable use of Laravel provided by the cache layer operation, take out the data set from the database for caching, reduce the pressure of the database, running in memory of professional cache software on the data read is much faster than the database. Posts = Cache: : remember (‘ index. The posts’, posts = Cache: : remember (‘ index. The posts, Post =Cache::remember(‘ index. Posts ‘, minutes = 30, function() {return Post::with(‘comments’, ‘tags’, ‘author’, ‘seo’)->whereHidden(0)->get(); }); Remember even caches the data association model as well, how convenient.
PHPhub has done an experiment with PHP 5.5+ OpCache to improve the performance of PHP applications by 50% or more without making any changes. 10. Front-end resource merging as the standard of optimization, a page should only load one CSS and one JS file, and the file name should be changed with the modification if the file can be easily CDN. Readers can also click on the link to receive the related learning benefits package if they feel like it.
Down and out programmer: how can PHP programmers invest in themselves – the path to PHP advancement
zhuanlan.zhihu.com] (zhuanlan.zhihu.com/p/344170997)
I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.