【问题标题】:How to precompile php opcache using command line?如何使用命令行预编译 php opcache?
【发布时间】:2020-10-17 05:22:12
【问题描述】:

我正在尝试使用控制台命令通过opcache.file_cache 功能预热 opcache。

我的 opcache.ini:

opcache.enable=true
opcache.enable_cli=true
opcache.memory_consumption=128
opcache.interned_strings_buffer=32
opcache.use_cwd=false
opcache.max_file_size=0
opcache.max_accelerated_files=32531
opcache.validate_timestamps=false
opcache.revalidate_freq=0
opcache.enable_file_override=true
opcache.optimization_level='0xFFFFFFFF'
opcache.file_update_protection=0
opcache.save_comments=false
opcache.file_cache='/tmp/.opcache'
opcache.file_cache_consistency_checks=false
opcache.log_verbosity_level=0

在我开始我的 php-fpm 进程之前,我通过从 clommand 行执行 scrpt 来编译文件:

$getDirs = static function () use ($argv): array {
    $dirs = $argv;
    unset($dirs[0]);
    return $dirs;
};

$files = \Symfony\Component\Finder\Finder::create()->in($getDirs())
    ->name('*.php')
    ->ignoreUnreadableDirs()
    ->notContains('#!/usr/bin/env php')
    ->notPath(get_included_files())
    ->files()
    ->followLinks();

$compiled = 0;
foreach ($files as $file) {
    try {
        if (!opcache_is_script_cached($file->getRealPath())) {
            opcache_compile_file($file->getRealPath());
        }

        $compiled++;
    } catch (Throwable $e) {
    }
}

echo 'Cached ' . $compiled . ' files' . PHP_EOL;

此脚本生成编译文件到指定的 (/tmp/.opcache) 目录。 然后我启动 php-fpm 进程,但没有使用预编译缓存,并且 opcache 每次重新启动 php-fpm 进程时都会编译缓存。

有没有办法在不使用http请求的情况下预编译opcache?

【问题讨论】:

  • 由于 web 和 CLI 版本是分开的,您需要在适当的环境中做一些事情来影响该环境的 opcache。

标签: php caching opcache


【解决方案1】:

可能有人会觉得这很有用 通过 CLI 为 php-fpm 预编译 opcache 确实有效 由于某种原因,将缓存文件加载到内存中需要花费太多时间(与内存中存储的操作码相比),但不会重新编译正在执行的 PHP 文件。

附:这个技巧甚至适用于不同的 opcache 配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 2016-02-02
    • 2010-10-25
    • 2011-12-13
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多