How to reduce scan time of Hyperf frame

Keywords: PHP Redis MySQL JSON

Reason

The Hyperf framework is designed to prevent the proxy cache from not updating after the user updates the component, resulting in a startup error. The following hooks have been added.

{
    "scripts": {
        "post-autoload-dump": [
            "init-proxy.sh"
        ]
    }
}

  

The init-proxy.sh script executes the PHP bin / hyperf.php Di: init proxy command to clean up the proxy cache and regenerate it.

$ composer init-proxy
> init-proxy.sh
../../
Runtime cleared
Scanning app ...
Scan app completed, took 195.76692581177 milliseconds.
Scanning vendor ...
Scan vendor completed, took 510.0839138031 milliseconds.
This command does not clear the runtime cache, If you want to delete them, use `vendor/bin/init-proxy.sh` instead.
Proxy class create success.
Finish!

 

In the above demonstration, we can clearly see that the time spent is not more than 1s, which is actually acceptable. But if you have a lot of models, this time can be an intolerable point. Such as the following.

$ composer init-proxy
> init-proxy.sh
../../
Runtime cleared
Scanning app ...
Scan app completed, took 3063.5998249054 milliseconds.
Scanning vendor ...
Scan vendor completed, took 490.39006233215 milliseconds.
This command does not clear the runtime cache, If you want to delete them, use `vendor/bin/init-proxy.sh` instead.
Proxy class create success.
Finish!

  

Solution

 

The following solution is based on the correct use of the Model. For example, do not use annotations in the Model. The detection method is to generate the proxy cache without excluding the Model directory and check whether to generate the Model related proxy.

Therefore, we can actively modify the scan directory of the Hyperf framework to exclude the model directory. Let's write a logic to modify annotations.php.

<?php

declare(strict_types=1);

use Symfony\Component\Finder\Finder;

return [
    'scan' => [
        'paths' => value(function () {
            $paths = [];
            $dirs = Finder::create()->in(BASE_PATH . '/app')
                ->depth('< 1')
                ->exclude(['Model']) // This is modified according to the actual situation
                ->directories();
            /** @var SplFileInfo $dir */
            foreach ($dirs as $dir) {
                $paths[] = $dir->getRealPath();
            }
            return $paths;
        }),
        'ignore_annotations' => [
            'mixin',
        ],
    ],
];

  

When we execute the command again, we will find that the time is greatly shortened.

Written in the end

 

Hyperf is a high-performance and flexible PHP cooperation framework based on Swoole 4.4 +. It has built-in cooperation server and a large number of common components. Its performance is improved qualitatively compared with the traditional framework based on PHP-FPM, providing ultra-high performance while maintaining extremely flexible scalability. The standard components are based on PSR standard Implementation, based on a powerful dependency injection design, ensures that most components or classes are replaceable and reusable.

 

In addition to the common MySQL client and Redis client of the collaboration version, the framework component library also provides you with Eloquent ORM, WebSocket server and client, JSON RPC server and client, GRPC server and client, Zipkin/Jaeger (OpenTracing) client, Guzzle HTTP client, Elasticsearch client, consult client, ETCD of the collaboration version The client, AMQP component, Apollo configuration center, alicloud ACM application configuration management, ETCD configuration center, current limiter based on token bucket algorithm, general connection pool, fuse, Swagger document generation, Swoole Tracker, Blade and Smarty view engine, Snowflake global ID generator and other components save the trouble of implementing the corresponding protocol version.

 

Hyperf also provides PSR-11-based dependency injection container, annotation, AOP aspect oriented programming, PSR-15-based middleware, custom process, PSR-14-based event manager, Redis/RabbitMQ message queue, automatic model cache, PSR-16-based cache, Crontab second level timed task, Translation internationalization, Validation validator And other very convenient functions, to meet the rich technical and business scenarios, out of the box.

For more information, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory, as long as you read it to ensure a higher salary (continuous update)

Posted by aisalen on Tue, 07 Apr 2020 01:16:34 -0700