2
头图

Performance has always been one of the biggest topics of Laravel PHP developers. Here we will speed up the framework again.

Basics (old clichés)

  • Cache, session, and queue are all driven Redis
  • Framework built-in cache

    • Route cache php artisan route:cache
    • Configure cache php artisan config:cache
  • Use cache based on interface idempotence

upgrade

  • OPcache

    • There is no special reason to go directly to this
    • And set opcache.validate_timestamps to 0 , so that the PHP code in your production environment will never be automatically updated, similar to other compiled languages, every time you deploy the code, you need to restart php-fpm to load the new code
    • For more information, please refer to this article https://gywbd.github.io/posts/2016/1/best-config-for-zend-opcache.html

Reduce unnecessary middleware

  • For example, Laravel now has built-in cross-domain middleware. If you only APP , there is no cross-domain problem at all. You can directly comment this middleware.
  • For example, the built-in API rate limiting interface may not be suitable for many project scenarios

Reduce service providers

  • For example, the interface does not need view service, Session service, password reset service, etc.

Of course, the key point to be API about is still the project mixed with 060ac8461716fb and Admin , which uses a full-stack framework. I believe there are also many projects that use Laravel-admin or Dcat Admin

I also wrote an article before on how to reduce your service providers, because since Laravel5.5 , you can register for third-party packages, and then Laravel automatically discovers these service providers
You can run this command to find out the 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.
  • Here you can clearly see the dcat/laravel-admin , we only need to go under the project root directory composer.json write the following configuration
 "extra": {
     "laravel": {
         "dont-discover": [
             "dcat/laravel-admin"
         ]
     }
 }
  • After you do this, execute php artisan package:discover again and you will find that dcat/laravel-admin disappeared
  • But this also means that we cannot use Admin , so we also need to add a condition to manually register the service provider
  • We can add the following code AppServiceProvider.php
<?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()
    {
        // 这个条件有很多种方式, 你甚至可以通过 url 来判断
        if (config('admin.enable')) {
            AliasLoader::getInstance()->alias('Admin', Admin::class);
            $this->app->register(AdminServiceProvider::class);
        }
    }
}
  • After the modification is completed, it is found that the performance has been improved by 40% (because Admin registered many routes and started many things)

machine:
2U4G machine CentOS Linux release 8.3.2011
Environment: (Use Laradock family bucket, turn on OPcache )

PHP_VERSION=7.4
PHP_FPM_INSTALL_OPCACHE=true
  • Finally, I looked for the operation and maintenance and asked for a API one of the 060ac846171aa6 servers


It can be seen that the effect after optimization is very good.

Links

https://www.shiguopeng.cn/archives/374
https://www.shiguopeng.cn/archives/507


seth-shi
1.8k 声望231 粉丝

当神不再是我们的信仰,那么信仰自己吧,努力让自己变好,不辜负自己的信仰!