开启OPcache和JIT

JIT依赖OPcache,所以要先载入OPcache扩展,然后启用OPcache和JIT
[root@localhost ~]# vim /program/php/php.ini
 
………………(此处省略内容若干)………………
 
;extension=tidy
;extension=xsl
;extension=zip
 
; 新增扩展
extension=amqp
extension=redis
 
; 启用opcache扩展
zend_extension=opcache
 
;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
 
………………(此处省略内容若干)………………
 
[dba]
;dba.default_handler=
 
[opcache]
; Determines if Zend OPCache is enabled
;opcache.enable=1
opcache.enable=1
 
; Determines if Zend OPCache is enabled for the CLI version of PHP
;opcache.enable_cli=0
opcache.enable_cli=1
 
; The OPcache shared memory storage size.
;opcache.memory_consumption=128
opcache.memory_consumption=128
 
; The amount of memory for interned strings in Mbytes.
;opcache.interned_strings_buffer=8
opcache.interned_strings_buffer=8
 
; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 1000000 are allowed.
;opcache.max_accelerated_files=10000
opcache.max_accelerated_files=10000
 
; The maximum percentage of "wasted" memory until a restart is scheduled.
;opcache.max_wasted_percentage=5
 
; When this directive is enabled, the OPcache appends the current working
; directory to the script key, thus eliminating possible collisions between
; files with the same name (basename). Disabling the directive improves
; performance, but may break existing applications.
;opcache.use_cwd=1
 
; When disabled, you must reset the OPcache manually or restart the
; webserver for changes to the filesystem to take effect.
; 自动刷新缓存,可选值:0=关闭|1=开启,如果关闭自动刷新则修改PHP文件后需手动调用opcache_reset()强制刷新缓存使修改生效。
;opcache.validate_timestamps=1
opcache.validate_timestamps=1
 
; How often (in seconds) to check file timestamps for changes to the shared
; memory storage allocation. ("1" means validate once per second, but only
; once per request. "0" means always validate)
; 自动刷新缓存频率(单位:秒),有用户访问页面时每隔N秒检查一次PHP文件,若检测到文件有更新则自动刷新缓存,
; 重要提醒:本配置仅在opcache.validate_timestamps=1时生效。
; 重要提醒:本配置设为0秒则用户每次访问页面都会检查文件是否有更新(相当于禁用缓存)。
;opcache.revalidate_freq=2
opcache.revalidate_freq=0
 
; opcache.validate_timestamps和opcache.revalidate_freq配置总结:
; 开发环境:opcache.validate_timestamps=1、opcache.revalidate_freq=0
; 生产环境:opcache.validate_timestamps=0、opcache.revalidate_freq=99999999  注:此时需手动刷新缓存
 
; JIT配置(非自带,需手动添加)
; 说明:opcache.jit配置项的可选值有:disable|on|off|tracing(1254)|function(1205)|4位数字
opcache.jit=1205
opcache.jit_buffer_size=64M
 
; Enables or disables file search in include_path optimization
;opcache.revalidate_path=0
 
; If disabled, all PHPDoc comments are dropped from the code to reduce the
; size of the optimized code.
;opcache.save_comments=1
 
; If enabled, compilation warnings (including notices and deprecations) will
; be recorded and replayed each time a file is included. Otherwise, compilation
; warnings will only be emitted when the file is first cached.
;opcache.record_warnings=0
 
; Allow file existence override (file_exists, etc.) performance feature.
;opcache.enable_file_override=0
 
; A bitmask, where each bit enables or disables the appropriate OPcache
; passes
;opcache.optimization_level=0x7FFFBFFF
 
;opcache.dups_fix=0
 
; The location of the OPcache blacklist file (wildcards allowed).
; Each OPcache blacklist file is a text file that holds the names of files
; that should not be accelerated. The file format is to add each filename
; to a new line. The filename may be a full path or just a file prefix
; (i.e., /var/www/x  blacklists all the files and directories in /var/www
; that start with 'x'). Line starting with a ; are ignored (comments).
;opcache.blacklist_filename=
 
; Allows exclusion of large files from being cached. By default all files
; are cached.
;opcache.max_file_size=0
 
; Check the cache checksum each N requests.
; The default value of "0" means that the checks are disabled.
;opcache.consistency_checks=0
 
; How long to wait (in seconds) for a scheduled restart to begin if the cache
; is not being accessed.
;opcache.force_restart_timeout=180
 
; OPcache error_log file name. Empty string assumes "stderr".
; 指定OPcache错误日志文件
;opcache.error_log=
opcache.error_log=/program/php/log/opcache.error.log
 
; All OPcache errors go to the Web server log.
; By default, only fatal errors (level 0) or errors (level 1) are logged.
; You can also enable warnings (level 2), info messages (level 3) or
; debug messages (level 4).
;opcache.log_verbosity_level=1
 
………………(此处省略内容若干)………………
 
[root@localhost ~]# service php-fpm restart # 重启php-fpm
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@localhost ~]#



以下是获取OPcache和JIT状态的代码

<?php
//========== 检查OPcache扩展是否已加载 ==========//
$loaded = extension_loaded('Zend OPcache');
if ($loaded === true) {
    echo 'OPcache扩展已加载' . PHP_EOL;
} else {
    echo 'OPcache扩展未加载' . PHP_EOL;
}


//========== 检查是否启用OPcache ==========//
// 说明①:加载OPcache扩展并不等于启用OPcache。
// 说明②:必须先加载OPcache扩展才能调用opcache_get_status()。
if (function_exists('opcache_get_status')) {
    $status = opcache_get_status(); // 未启用OPcache时返回false
    if (isset($status['opcache_enabled']) && $status['opcache_enabled'] === true) {
        echo 'OPcache功能已启用' . PHP_EOL;
    } else {
        echo 'OPcache功能未启用' . PHP_EOL;
    }
}


//========== 检查是否启用JIT ==========//
// 说明:JIT依赖OPcache,如果未启用OPcache那么JIT肯定也无法启用。
if (isset($status['opcache_enabled']) && $status['opcache_enabled'] === true) {
    $jit = $status['jit'];
    if ($jit['enabled'] === true && $jit['on'] === true) {
        echo 'JIT功能已启用' . PHP_EOL;
    } else {
        echo 'JIT功能未启用' . PHP_EOL;
    }
}


//========== 查询OPcache自动刷新频率 ==========//
if (isset($status['opcache_enabled']) && $status['opcache_enabled'] === true) {
    if ((int)ini_get('opcache.validate_timestamps') === 1) {
        $freq = (int)ini_get('opcache.revalidate_freq');
        if ($freq > 0) {
            echo "OPcache自动刷新频率:每{$freq}秒刷新" . PHP_EOL;
        } else {
            echo 'OPcache自动刷新频率:每次访问均刷新' . PHP_EOL;
        }
    } else {
        echo 'OPcache自动刷新频率:不自动刷新' . PHP_EOL;
    }
}


//========== 手动刷新OPcache ==========//
if (function_exists('opcache_reset')) {
    $reset = opcache_reset();
    if ($reset === true) {
        echo '手动刷新OPcache成功' . PHP_EOL;
    } else {
        echo '手动刷新OPcache失败' . PHP_EOL;
    }
}

OPcache不是文件缓存,而是内存缓存,并且是使用共享内存(SHM)来缓存。

OPcache只缓存PHP脚本,如果使用了模板引擎,那么并不会缓存模板文件。

启用OPcache后会将PHP脚本缓存,这时如果修改了PHP脚本必须刷新缓存才能使修改生效,所以网站后台最好实现手动刷新缓存功能,这样每次修改了PHP脚本就不用远程登录服务器重启php-fpm,只要登录网站后台刷新缓存即可。

Copyright © 2024 码农人生. All Rights Reserved