Pee*_*Haa 370
默认情况下,在PHP5.5 +上编译OpCache.但是默认情况下它被禁用.要在PHP5.5 +中开始使用OpCache,首先必须启用它.为此,您必须执行以下操作.
将以下行添加到您的php.ini
:
zend_extension=/full/path/to/opcache.so (nix)
zend_extension=C:\path\to\php_opcache.dll (win)
Run Code Online (Sandbox Code Playgroud)
请注意,当路径包含空格时,您应将其包装在引号中:
zend_extension="C:\Program Files\PHP5.5\ext\php_opcache.dll"
Run Code Online (Sandbox Code Playgroud)
另请注意,您必须使用该zend_extension
指令而不是"normal" extension
指令,因为它会影响实际的Zend引擎(即运行PHP的引擎).
目前有四种功能可以使用:
opcache_get_configuration()
:返回包含OpCache当前使用的配置的数组.这包括所有ini设置以及版本信息和列入黑名单的文件.
var_dump(opcache_get_configuration());
Run Code Online (Sandbox Code Playgroud)
opcache_get_status()
:这将返回一个数组,其中包含有关缓存当前状态的信息.此信息将包括以下内容:缓存所处的状态(启用,重新启动,完全等),内存使用情况,命中,未命中以及一些更有用的信息.它还将包含缓存的脚本.
var_dump(opcache_get_status());
Run Code Online (Sandbox Code Playgroud)
opcache_reset()
:重置整个缓存.这意味着所有可能的缓存脚本将在下次访问时再次解析.
opcache_reset();
Run Code Online (Sandbox Code Playgroud)
opcache_invalidate()
:使特定的缓存脚本无效.这意味着在下次访问时将再次解析脚本.
opcache_invalidate('/path/to/script/to/invalidate.php', true);
Run Code Online (Sandbox Code Playgroud)
创建了一些GUI来帮助维护OpCache并生成有用的报告.这些工具利用了上述功能.
OpCacheGUI
免责声明我是这个项目的作者
特征:
截图:
网址:https://github.com/PeeHaa/OpCacheGUI
opcache状态
特征:
截图:
网址:https://github.com/rlerdorf/opcache-status
opcache贵
特征:
截图:
网址:https://github.com/amnuts/opcache-gui
Dan*_*ack 152
由于OPcache旨在取代APC模块,因此无法在PHP中并行运行它们.这对于缓存PHP操作码很好,因为它既不会影响您编写代码的方式.
但是,这意味着如果您当前正在使用APC来存储其他数据(通过该apc_store()
功能),那么如果您决定使用OPCache,则无法执行此操作.
您将需要使用另一个库,例如APCu或Yac,它们都将数据存储在共享的PHP内存中,或者切换到使用memcached之类的东西,它将数据存储在内存中,与PHP分开.
此外,OPcache没有相当于APC中存在的上传进度表.相反,您应该使用会话上载进度.
对于OPcache的文档,可以发现这里所有列出的配置选项在这里.建议的设置是:
; 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
Run Code Online (Sandbox Code Playgroud)
如果您使用任何使用代码注释的库或代码,则必须启用保存注释:
opcache.save_comments=1
Run Code Online (Sandbox Code Playgroud)
如果禁用,则从代码中删除所有PHPDoc注释以减小优化代码的大小.禁用"Doc Comments"可能会破坏一些现有的应用程序和框架(例如Doctrine,ZF2,PHPUnit)
Tsc*_*cka 19
因为我使用opcache,所以我要花两美分.
我已经建立了一个包含大量字段和验证方法以及枚举的扩展框架,以便能够与我的数据库进行通信.
没有opcache
当使用没有opcache的脚本时,我会在2.8秒内将9000个请求推送到apache服务器,它会在90-100%cpu上最大化70-80秒,直到它赶上所有请求.
Total time taken: 76085 milliseconds(76 seconds)
启用opcache
启用opcache后,它以25-30%的CPU时间运行大约25秒,并且永远不会超过25%的CPU使用率.
Total time taken: 26490 milliseconds(26 seconds)
我已经制作了一个opcache黑名单文件来禁用除框架之外的所有内容的缓存,这些内容都是静态的,不需要更改功能.我明确选择框架文件,以便我可以开发而不必担心重新加载/验证缓存文件.将所有缓存都保存在请求的总数上25546 milliseconds
这显着扩展了我每秒可以处理的数据/请求数量,而服务器甚至不会出汗.