简体   繁体   English

如何使用命令行预编译 php opcache?

[英]How to precompile php opcache using command line?

I'm trying to warm up opcache via opcache.file_cache feature using console command.我正在尝试使用控制台命令通过opcache.file_cache功能预热 opcache。

My opcache.ini:我的 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

before I start my php-fpm process I compile files by executing scrpt from clommand line:在我开始我的 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;

This script generates compiled files to specified ( /tmp/.opcache ) directory.该脚本生成编译文件到指定的 ( /tmp/.opcache ) 目录。 Then I start php-fpm process, but no precompiled cache is not used and opcache compiles cache every time php-fpm process being restarted.然后我启动 php-fpm 进程,但没有使用预编译缓存,并且 opcache 每次重新启动 php-fpm 进程时都会编译缓存。

Is there a way to precompile opcache without using http requests?有没有办法在不使用 http 请求的情况下预编译 opcache?

May be someone will find this useful Precompiling opcache for php-fpm via CLI does work For some reason it takes too much time (compared with in-memory stored Opcodes) to load cached files into memory, but no recompilation of PHP files being executed.可能有人会发现这个有用的通过 CLI 为 php-fpm 预编译 opcache 确实有效由于某种原因,将缓存文件加载到 memory 需要花费太多时间(与内存中存储的操作码相比),但不会重新编译正在执行的 PHP 文件。

PS This trick works even for different opcache confgurations. PS 这个技巧甚至适用于不同的 opcache 配置。

add below setting添加以下设置

opcache.file_cache_only=1 opcache.file_cache_only=1

into your php.ini进入你的 php.ini

because opcache.file_cache_only is off.因为 opcache.file_cache_only 已关闭。 opcache only using file cache on memory fulling. opcache 仅使用 memory 上的文件缓存进行填充。 So your want opcache must be use file cache.所以你想要的 opcache 必须使用文件缓存。 Please turn on opcache.file_cache_only=1.请开启 opcache.file_cache_only=1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM