PHP 5.5已发布,其中包含一个名为OPCache的新代码缓存模块,但似乎没有任何文档可供参考。
那么,它的文档在哪里,我该如何使用OPcache?
PHP 5.5已发布,其中包含一个名为OPCache的新代码缓存模块,但似乎没有任何文档可供参考。
那么,它的文档在哪里,我该如何使用OPcache?
OpCache在PHP5.5+上默认编译,但默认情况下被禁用。要开始使用PHP5.5+中的OpCache,您需要首先启用它。为此,您需要执行以下操作。
将以下行添加到您的 php.ini
文件中:
zend_extension=/full/path/to/opcache.so (nix)
zend_extension=C:\path\to\php_opcache.dll (win)
请注意,当路径中包含空格时,您应该将其用引号括起来:
zend_extension="C:\Program Files\PHP5.5\ext\php_opcache.dll"
还要注意,您必须使用zend_extension
指令,而不是“普通”的extension
指令,因为它会影响实际运行PHP的Zend引擎。
目前有四个函数可供使用:
opcache_get_configuration()
:返回一个数组,其中包含OpCache当前使用的配置。这包括所有ini设置以及版本信息和黑名单文件。
var_dump(opcache_get_configuration());
opcache_get_status()
:该函数将返回一个包含当前缓存状态信息的数组。这些信息包括:缓存处于何种状态(已启用、重启中、已满等)、内存使用情况、命中数、未命中数以及其他一些有用的信息。此外,它还会包含缓存的脚本。
var_dump(opcache_get_status());
opcache_reset()
:重置整个缓存。这意味着所有可能被缓存的脚本将在下一次访问时重新解析。
opcache_reset();
opcache_invalidate()
:使特定缓存脚本失效,即该脚本会在下一次访问时重新解析。
opcache_invalidate('/path/to/script/to/invalidate.php', true);
有一些GUI工具可用于帮助维护OpCache并生成有用的报告。这些工具利用了上述功能。
OpCacheGUI
免责声明:本人是该项目的作者
特点:
截图:
网址:https://github.com/PeeHaa/OpCacheGUI
opcache-status
特点:
截图:
网址:https://github.com/rlerdorf/opcache-status
opcache-gui
特点:
截图:
Xdebug
扩展程序,则必须在 OpCache
扩展程序之后加载。 - Yousha Aleayoub由于OPcache是设计用来取代APC模块的,所以在PHP中无法同时运行它们。这对于缓存PHP opcode来说没有问题,因为它们都不会影响你编写代码的方式。
然而,这意味着如果你目前正在使用APC来存储其他数据(通过apc_store()
函数),如果你决定使用OPCache,你将无法继续这样做。
你需要使用另一个库,比如APCu或Yac,它们都将数据存储在共享的PHP内存中,或者切换到使用像memcached这样的东西,它将数据存储在与PHP分开的进程中的内存中。
另外,OPcache没有APC中的上传进度条的等效功能。相反,你应该使用Session Upload Progress。
; Sets how much memory to use
opcache.memory_consumption=128
;Sets how much memory should be used by OPcache for storing internal strings
;(e.g. classnames and the files they are contained in)
opcache.interned_strings_buffer=8
; The maximum number of files OPcache will cache
opcache.max_accelerated_files=4000
;How often (in seconds) to check file timestamps for changes to the shared
;memory storage allocation.
opcache.revalidate_freq=60
;If enabled, a fast shutdown sequence is used for the accelerated code
;The fast shutdown sequence doesn't free each allocated block, but lets
;the Zend Engine Memory Manager do the work.
opcache.fast_shutdown=1
;Enables the OPcache for the CLI version of PHP.
opcache.enable_cli=1
opcache.save_comments=1
我要分享一下我的个人经验,我使用opcache。
我创建了一个包含许多字段、验证方法和枚举的框架来与我的数据库交互。
没有启用opcache
当我在没有启用opcache的情况下将9000个请求发送到Apache服务器时,CPU利用率会在70-80秒达到90-100%,直到它赶上所有的请求。
总共用时:76085毫秒(76秒)
启用opcache后
启用opcache后,CPU利用率只有25-30%,大约需要25秒的时间,而且不会超过25%的CPU使用率。
总共用时:26490毫秒(26秒)
我编写了一个opcache黑名单文件,以禁用缓存除静态框架之外的所有内容,这些静态框架不需要更改功能。我明确选择只为框架文件启用缓存,这样我就可以在开发时不用担心重新加载/验证缓存文件。缓存所有内容可以节省一秒钟的请求时间25546毫秒
这显著扩展了我每秒处理数据/请求的能力,甚至不需要让服务器劳累。
opcache.revalidate_freq=60;
确定文件可以在内存中存活的秒数。当时间到了,它会重新编译该文件。 - Tschallackaopcache.revalidate_freq
控制着脚本被检查更改的频率(基于其时间戳是否更改)。因此,如果一个脚本的时间戳与上次编译时保持不变,则它将不会被重新编译。这一切都是在假设您没有更改默认启用的 opcache.validate_timestamps
设置的情况下进行的。 - jjlin如果您正在使用Amazon Linux(在RedHat或CentOS上应该是相同的)上的PHP 5.6:
yum install php56-opcache
然后重新启动apache。
我在设置 Moodle 时遇到了这个问题。我在 php.ini 文件中添加了以下几行内容。
zend_extension=C:\xampp\php\ext\php_opcache.dll
[opcache]
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 4000
opcache.revalidate_freq = 60
; Required for Moodle
opcache.use_cwd = 1
opcache.validate_timestamps = 1
opcache.save_comments = 1
opcache.enable_file_override = 0
; If something does not work in Moodle
;opcache.revalidate_path = 1 ; May fix problems with include paths
;opcache.mmap_base = 0x20000000 ; (Windows only) fix OPcache crashes with event id 487
; Experimental for Moodle 2.6 and later
;opcache.fast_shutdown = 1
;opcache.enable_cli = 1 ; Speeds up CLI cron
;opcache.load_comments = 0 ; May lower memory use, might not be compatible with add-ons and other apps
extension=C:\xampp\php\ext\php_intl.dll
[intl]
intl.default_locale = en_utf8
intl.error_level = E_WARNING