如何使用 PHP OPCache?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17224798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use PHP OPCache?
提问by Danack
PHP 5.5 has been released and it features a new code caching module called OPCache, but there doesn't appear to be any documentation for it.
PHP 5.5 已经发布,它具有一个名为 OPCache 的新代码缓存模块,但似乎没有任何相关文档。
So where is the documentation for it and how do I use OPcache?
那么它的文档在哪里以及如何使用 OPcache?
回答by PeeHaa
Installation
安装
OpCache is compiled by default on PHP5.5+. However it is disabled by default. In order to start using OpCache in PHP5.5+ you will first have to enable it. To do this you would have to do the following.
OpCache 在 PHP5.5+ 上默认编译。但是默认情况下它是禁用的。为了在 PHP5.5+ 中开始使用 OpCache,您首先必须启用它。为此,您必须执行以下操作。
Add the following line to your php.ini
:
将以下行添加到您的php.ini
:
zend_extension=/full/path/to/opcache.so (nix)
zend_extension=C:\path\to\php_opcache.dll (win)
Note that when the path contains spaces you should wrap it in quotes:
请注意,当路径包含空格时,您应该将其用引号括起来:
zend_extension="C:\Program Files\PHP5.5\ext\php_opcache.dll"
Also note that you will have to use the zend_extension
directive instead of the "normal" extension
directive because it affects the actual Zend engine (i.e. the thing that runs PHP).
另请注意,您将不得不使用该zend_extension
指令而不是“普通”extension
指令,因为它会影响实际的 Zend 引擎(即运行 PHP 的东西)。
Usage
用法
Currently there are four functions which you can use:
目前有四种功能可供您使用:
opcache_get_configuration()
:
opcache_get_configuration()
:
Returns an array containing the currently used configuration OpCache uses. This includes all ini settings as well as version information and blacklisted files.
返回一个包含 OpCache 当前使用的配置的数组。这包括所有 ini 设置以及版本信息和列入黑名单的文件。
var_dump(opcache_get_configuration());
opcache_get_status()
:
opcache_get_status()
:
This will return an array with information about the current status of the cache. This information will include things like: the state the cache is in (enabled, restarting, full etc), the memory usage, hits, misses and some more useful information. It will also contain the cached scripts.
这将返回一个包含有关缓存当前状态信息的数组。此信息将包括以下内容:缓存所处的状态(启用、重新启动、已满等)、内存使用情况、命中、未命中和一些更有用的信息。它还将包含缓存的脚本。
var_dump(opcache_get_status());
opcache_reset()
:
opcache_reset()
:
Resets the entire cache. Meaning all possible cached scripts will be parsed again on the next visit.
重置整个缓存。这意味着所有可能的缓存脚本将在下次访问时再次解析。
opcache_reset();
opcache_invalidate()
:
opcache_invalidate()
:
Invalidates a specific cached script. Meaning the script will be parsed again on the next visit.
使特定的缓存脚本无效。这意味着脚本将在下次访问时再次解析。
opcache_invalidate('/path/to/script/to/invalidate.php', true);
Maintenance and reports
维护和报告
There are some GUI's created to help maintain OpCache and generate useful reports. These tools leverage the above functions.
创建了一些 GUI 来帮助维护 OpCache 并生成有用的报告。这些工具利用了上述功能。
OpCacheGUI
操作缓存GUI
Disclaimer I am the author of this project
免责声明我是这个项目的作者
Features:
特征:
- OpCache status
- OpCache configuration
- OpCache statistics
- OpCache reset
- Cached scripts overview
- Cached scripts invalidation
- Multilingual
- Mobile device support
- Shiny graphs
- OpCache 状态
- OpCache 配置
- OpCache 统计信息
- OpCache 重置
- 缓存脚本概述
- 缓存脚本失效
- 多种语言
- 移动设备支持
- 闪亮的图表
Screenshots:
截图:
URL: https://github.com/PeeHaa/OpCacheGUI
网址:https: //github.com/PeeHaa/OpCacheGUI
opcache-status
opcache-状态
Features:
特征:
- OpCache status
- OpCache configuration
- OpCache statistics
- Cached scripts overview
- Single file
- OpCache 状态
- OpCache 配置
- OpCache 统计信息
- 缓存脚本概述
- 单个文件
Screenshot:
截屏:
URL: https://github.com/rlerdorf/opcache-status
网址:https: //github.com/rlerdorf/opcache-status
opcache-gui
opcache-gui
Features:
特征:
- OpCache status
- OpCache configuration
- OpCache statistics
- OpCache reset
- Cached scripts overview
- Cached scripts invalidation
- Automatic refresh
- OpCache 状态
- OpCache 配置
- OpCache 统计信息
- OpCache 重置
- 缓存脚本概述
- 缓存脚本失效
- 自动刷新
Screenshot:
截屏:
回答by Danack
OPcache replaces APC
OPcache 替代 APC
Because OPcache is designed to replace the APC module, it is not possible to run them in parallel in PHP. This is fine for caching PHP opcode as neither affects how you write code.
由于 OPcache 旨在替代 APC 模块,因此无法在 PHP 中并行运行它们。这对于缓存 PHP 操作码很好,因为它们都不会影响您编写代码的方式。
However it means that if you are currently using APC to store other data (through the apc_store()
function) you will not be able to do that if you decide to use OPCache.
然而,这意味着如果您当前正在使用 APC 来存储其他数据(通过该apc_store()
函数),如果您决定使用 OPCache,您将无法这样做。
You will need to use another library such as either APCuor Yacwhich both store data in shared PHP memory, or switch to use something like memcached, which stores data in memory in a separate process to PHP.
您将需要使用另一个库,例如APCu或Yac,它们都将数据存储在共享的 PHP 内存中,或者切换到使用诸如 memcached 之类的东西,它将数据存储在 PHP 的单独进程中的内存中。
Also, OPcache has no equivalent of the upload progress meter present in APC. Instead you should use the Session Upload Progress.
此外,OPcache 没有相当于 APC 中的上传进度表。相反,您应该使用Session Upload Progress。
Settings for OPcache
OPcache 的设置
The documentation for OPcache can be found herewith all of the configuration options listed here. The recommended settings are:
对于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
If you use any library or code that uses code annotations you must enable save comments:
如果您使用任何使用代码注释的库或代码,则必须启用保存注释:
opcache.save_comments=1
If disabled, all PHPDoc comments are dropped from the code to reduce the size of the optimized code. Disabling "Doc Comments" may break some existing applications and frameworks (e.g. Doctrine, ZF2, PHPUnit)
如果禁用,所有 PHPDoc 注释将从代码中删除以减少优化代码的大小。禁用“Doc Comments”可能会破坏一些现有的应用程序和框架(例如 Doctrine、ZF2、PHPUnit)
回答by Tschallacka
I am going to drop in my two cents for what I use opcache.
对于我使用的 opcache,我要花两分钱。
I have made an extensive framework with a lot of fields and validation methods and enums to be able to talk to my database.
我已经制作了一个包含许多字段和验证方法以及枚举的广泛框架,以便能够与我的数据库对话。
Without opcache
没有 opcache
When using this script without opcache and I push 9000 requests in 2.8 seconds to the apache server it maxes out at 90-100% cpu for 70-80 seconds until it catches up with all the requests.
当在没有 opcache 的情况下使用这个脚本并且我在 2.8 秒内将 9000 个请求推送到 apache 服务器时,它会在 90-100% 的 CPU 下达到最大值,持续 70-80 秒,直到它赶上所有请求。
Total time taken: 76085 milliseconds(76 seconds)
Total time taken: 76085 milliseconds(76 seconds)
With opcache enabled
启用 opcache
With opcache enabled it runs at 25-30% cpu time for about 25 seconds and never passes 25% cpu use.
启用 opcache 后,它会以 25-30% 的 CPU 时间运行约 25 秒,并且永远不会超过 25% 的 CPU 使用率。
Total time taken: 26490 milliseconds(26 seconds)
Total time taken: 26490 milliseconds(26 seconds)
I have made an opcache blacklist file to disable the caching of everything except the framework which is all static and doesnt need changing of functionality. I choose explicitly for just the framework files so that I could develop without worrying about reloading/validating the cache files. Having everything cached saves a second on the total of the requests 25546 milliseconds
我制作了一个 opcache 黑名单文件来禁用所有内容的缓存,除了所有静态且不需要更改功能的框架。我只为框架文件明确选择,这样我就可以开发而不必担心重新加载/验证缓存文件。缓存所有内容可以节省一秒钟的请求总数25546 milliseconds
This significantly expands the amount of data/requests I can handle per second without the server even breaking a sweat.
这显着扩展了我每秒可以处理的数据/请求量,而服务器甚至不会出汗。
回答by Roger Dueck
With PHP 5.6 on Amazon Linux (should be the same on RedHat or CentOS):
在 Amazon Linux 上使用 PHP 5.6(在 RedHat 或 CentOS 上应该相同):
yum install php56-opcache
and then restart apache.
然后重启apache。
回答by Anoop Toffy
I encountered this when setting up moodle. I added the following lines in the php.ini file.
我在设置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