In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to improve the performance of Laravel applications". In daily operation, I believe many people have doubts about how to improve the performance of Laravel applications. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to improve the performance of Laravel applications"! Next, please follow the editor to study!
Cache Profil
The configuration items for laravel are distributed over dozens of configuration files, and including each file in each request consumes performance. To merge all the configuration files into one, you can use:
Php artisan config:cache
Keep in mind that modifying the configuration file will not affect the existing profile cache. To flush the cache, you can repeat the above command. If you want to completely clear the cache, execute:
Php artisan config:clear
Routing cach
In laravel, routing also requires expensive overhead. Cache the routes.php file with the following command:
Php artisan route:cache
Note that it does not apply to closures. If you are using closures, this is a good opportunity to move them to the controller, because the artisan command throws an exception when trying to compile the path bound to the closure instead of the correct controller method.
Like configuring caching, there is no effect on any changes to routes.php. To flush the cache, run the above command each time you change the path file. To clean up the route cache completely, run the following command:
Php artisan route:clear
Class mapping loading optimization
In a medium-sized project, it is normal to have hundreds of PHP source files. Because of good programming habits, we will separate the code, and each php file has its own responsibility. Of course, this is not without drawbacks, Laravel must load these hundreds of files for each request, which is a very performance-consuming thing.
Therefore, a better way is to declare which files need to be loaded every time the user requests (such as service provider, middleware, etc.), and then write these files that need to be loaded into the same file each time, reducing the number of include files.
This is similar to javascript merging files into an undifferentiated (webpack, gulp), which reduces requests from the browser server.
If you need to add additional source files, you can declare them in the files key of config / compile.php.
When you set up the files that need to be loaded for each request, they will be written to the same file, reducing the performance consumption of loading files.
Php artisan optimize-force
Optimize the automatic loading of composer
This applies not only to laravel, but also to any application that uses composer.
I'll first explain how the PSR-4 autoloader works, and then show you what commands you should run to optimize it. If you are not interested in understanding how composer works, I suggest you skip directly to the paragraph on console commands.
When you request the App\ Controllers\ AuthController class from compsoser, it first searches for the direct association in the class map. Classmap is an associative array of classes and files from 1 to 1. Of course, since you didn't manually add the Login class and its related files to the class map, composer will continue to search in the namespace.
Because App is a PSR-4 namespace, provided with Laravel by default, and associated with the app/ folder, composer will attempt to convert the PSR-4 class name to a file name using a basic string manipulation procedure. Finally, it guesses that App\ Controllers\ AuthController must be in the AuthController.php file, in the Controllers/ folder, which happens to be in the namespace folder, app/.
All this hard work is just to get that the App\ Controllers\ AuthController class exists in the app/Controllers/AuthController.php file. To have composer scan the entire application and create a direct 1-to-1 association between classes and files, run the following command:
Composer dumpautoload-o
Remember, if you've already run php artisan optimize-- force, you don't have to run this function anymore. Because the optimization command has told composer to create an optimized autoloader.
JIT compiler (just-in-time compiler)
PHP is not naturally understood by computers. You can't compile it into bytecode and let the computer run. PHP must go through an intermediary, such as the Zend engine, which interprets the PHP file and executes the corresponding C routines. As you can imagine, it's very slow. Every time your server executes a PHP file, you must convert it to tokens-a process that is done and interpreted by the AST parser. Unfortunately, the parser must compile the PHP file every time, even if it gets the same result every time.
To make your application faster, you need a method that compiles once and runs for a lifetime, and that's what a JIT compiler does.
The recommended JIT compiler for Laravel is HHVM, created by Facebook and widely used. Wikipedia, Etsy, and thousands of other projects are also using it.
Use faster caching and session drivers
Saving session in a file is fast and elegant enough, and it has been done since the beginning of PHP. But if you are looking for performance, then the file system is one thing you need to pay attention to because it is slow. A better approach is to store cache and session in memory because it provides an efficient way to read and write data. Fortunately, laravel supports some memory-based cache and session drivers.
My advice is to use memcached as the driver for cache and session, but you can choose whatever you like, as long as it works based on memory.
To change the session driver, check the "driver" entry in the following file:
App/config/session.php
To change the cache driver, check the "driver" entry in the following file:
App/config/cache.php
Do not underestimate the improvement in query speed brought about by optimizing query statements
As you can see, most optimizations use caching at different levels. But when faced with database optimization, you should not rely on caching. Caching should be the last resort to optimize queries.
Cache query results
MySQL won't do it for you, and it's not as good as you do it yourself. Of course, you certainly won't cache the results of every query in the application, look at the statistics, those high-frequency query statements in the application, do they really need to be executed frequently? Wouldn't it be better to run it every 15 minutes and provide the same results to the user?
Removing the removing method from the query constructor is a good thing (it used to be a good feature, but not good enough-people seem to overestimate its effectiveness). Then you can use the Cache::remember method more often, like this:
Posts = Cache::remember ('index.posts', 30, function () {return Post::with (' comments', 'tags',' author', 'seo')-> whereHidden (0)-> get ();}); at this point, the study on "how to improve the performance of Laravel applications" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.