nytris/boost

维护者

详细信息

github.com/nytris/boost

源代码

问题

安装次数: 5,353

依赖: 1

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:项目

v0.1.1 2024-08-21 01:47 UTC

This package is auto-updated.

Last update: 2024-09-23 00:24:54 UTC


README

Build Status

提升PHP性能,尤其是在开启open_basedir的情况下。

为什么?

  • open_basedir禁用了PHP的realpath和stat缓存,这个库以可配置的方式重新实现了它们。
  • 即使open_basedir被禁用,原生的缓存也只存储在每个进程内。这个库允许使用PSR兼容的缓存来存储它们。

注意,对于原生的文件系统包装器(当这个库不被使用时)

  • stat缓存只保留一个文件,最新的stat结果。
  • 还有一个类似的单个状态缓存用于lstat结果。

当使用时,这个库缓存了所有访问的文件的状态,而不仅仅是最近的一个。

使用方法

使用Composer安装此包

$ composer require nytris/boost

当使用Nytris平台(推荐)时

配置Nytris平台

nytris.config.php

<?php

declare(strict_types=1);

use Nytris\Boost\BoostPackage;
use Nytris\Boot\BootConfig;
use Nytris\Boot\PlatformConfig;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$bootConfig = new BootConfig(new PlatformConfig(__DIR__ . '/var/cache/nytris/'));

$bootConfig->installPackage(new BoostPackage(
    // Allows changing to avoid collisions if required.
    realpathCacheKey: 'realpath_key',

    // Using Symfony Cache adapter as an example.
    realpathCachePoolFactory: fn (string $cachePath) => new FilesystemAdapter(
        'realpath',
        0,
        $cachePath
    ),

    // Allows changing to avoid collisions if required.
    statCacheKey: 'stat_key',

    // Using Symfony Cache adapter as an example.
    statCachePoolFactory: fn (string $cachePath) => new FilesystemAdapter(
        'stat',
        0,
        $cachePath
    ),

    // Whether to hook `clearstatcache(...)`.
    hookBuiltinFunctions: true
));

return $bootConfig;

当独立使用Boost时

尽可能早地将Boost加载到您的应用程序中,例如在/bootstrap.php

<?php

declare(strict_types=1);

use Nytris\Boost\Boost;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

require __DIR__ . '/vendor/autoload.php';

// Install Nytris Boost as early as possible so that as many files as possible are cached.
if (getenv('ENABLE_NYTRIS_BOOST') === 'yes') {
    (new Boost(
        realpathCachePool: new FilesystemAdapter(
            'nytris.realpath',
            0,
            __DIR__ . '/var/cache/'
        ),
        statCachePool: new FilesystemAdapter(
            'nytris.stat',
            0,
            __DIR__ . '/var/cache/'
        ),
        hookBuiltinFunctions: false
    ))->install();
}

...

另请参阅