Performance has always been one of the biggest topics in Laravel, but it has certainly provided a lot of different knowledge for many PHP developers, and here’s another acceleration for the framework.
The basics (a cliche)
- Caches, sessions, queues are all used
Redis
drive -
Built-in caching for the framework
- The routing cache
php artisan route:cache
- Configure the cache
php artisan config:cache
- The routing cache
- Use the cache based on the idempotency of the interface
upgrade
-
OPcache
- No particular reason to go straight to this
- And the
opcache.validate_timestamps
Set to0
, which allows you to produce environmentsPHP
Code is never automatically updated, and like other compiled languages, every time code is deployed, it needs to be restartedphp-fpm
To load the new code - More this article https://gywbd.github.io/posts/2016/1/best-config-for-zend-opcache.html for reference
Reduce unnecessary middleware
- Such as
Laravel
Cross-domain middleware is now built in, if only forAPP
By providing interfaces, there is no cross-domain problem at all, and the middleware can be annotated directly - Like the built-in
API
Rate-limiting interfaces may not be appropriate for many project scenarios
Decrease service providers
- For example, interfaces don’t need view services,
Session
Services, password reset services, etc
Projects that mix API and Admin use a full-stack framework. There are also projects that use laravel-admin or Dcat Admin
I also wrote about how to reduce your number of service providers, since since Laravel5.5, any third party packages that need to be registered can be registered on their own. Laravel then automatically discovers these service providers and runs this command to find out which service providers you have registered:
php artisan package:discover
Discovered Package: dcat/laravel-admin
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
- You can see it clearly here
dcat/laravel-admin
, we just need to go to the project root directorycomposer.json
Write the following configuration
"extra": {
"laravel": {
"dont-discover": [
"dcat/laravel-admin"
]
}
}
- When you have done so, execute
php artisan package:discover
You will finddcat/laravel-admin
disappeared - But that also means we can’t use it
Admin
So we also need to add a condition to manually register the service provider - We can do that
AppServiceProvider.php
Add the following code
<? php namespace App\Providers; use Dcat\Admin\Admin; use Dcat\Admin\AdminServiceProvider; use Illuminate\Foundation\AliasLoader; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { // } /** * Bootstrap any application services. * * @return void */ public function boot() { // If (config('admin.enable')) {aliasLoader ::getInstance()->alias(' admin ', admin ::class); $this->app->register(AdminServiceProvider::class); }}}
- After the modification, you find that the performance is good
40%
Because ofAdmin
Registered a lot of routes, started a lot of things)
Machine:CentOS Linux Release 8.3.2011 for 2u4G machines
Environment: (UseLaradock
Family bucket, openOPcache
)
7.4 PHP_FPM_INSTALL_OPCACHE PHP_VERSION = = true
- Finally, I asked for one of them
API
Diagram of server
As you can see, it looks pretty good.
Links
https://www.shiguopeng.cn/archives/374
https://www.shiguopeng.cn/archives/507