简体   繁体   English

如何使用PHP OPCache?

[英]How to use PHP OPCache?

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?

Installation 安装

OpCache is compiled by default on PHP5.5+. 默认情况下,在PHP5.5 +上编译OpCache。 However it is disabled by default. 但是默认情况下它被禁用。 In order to start using OpCache in PHP5.5+ you will first have to enable it. 要在PHP5.5 +中开始使用OpCache,首先必须启用它。 To do this you would have to do the following. 为此,您必须执行以下操作。

Add the following line to your php.ini : 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 (ie the thing that runs PHP). 另请注意,您必须使用zend_ extension指令而不是“normal” 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. 返回包含OpCache当前使用的配置的数组。 This includes all ini settings as well as version information and blacklisted files. 这包括所有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. 创建了一些GUI来帮助维护OpCache并生成有用的报告。 These tools leverage the above functions. 这些工具利用了上述功能。

OpCacheGUI OpCacheGUI

Disclaimer I am the author of this project 免责声明我是这个项目的作者

Features: 特征:

  • OpCache status OpCache状态
  • OpCache configuration OpCache配置
  • OpCache statistics OpCache统计
  • OpCache reset OpCache重置
  • Cached scripts overview 缓存脚本概述
  • Cached scripts invalidation 缓存脚本失效
  • Multilingual 多种语言
  • Mobile device support 移动设备支持
  • Shiny graphs 闪亮的图表

Screenshots: 截图:

状态

缓存的脚本

图

mobilr

URL: https://github.com/PeeHaa/OpCacheGUI 网址: https//github.com/PeeHaa/OpCacheGUI

opcache-status opcache状态

Features: 特征:

  • OpCache status OpCache状态
  • OpCache configuration OpCache配置
  • OpCache statistics OpCache统计
  • Cached scripts overview 缓存脚本概述
  • Single file 单个文件

Screenshot: 截图:

状态

URL: https://github.com/rlerdorf/opcache-status 网址: https//github.com/rlerdorf/opcache-status

opcache-gui opcache贵

Features: 特征:

  • OpCache status OpCache状态
  • OpCache configuration OpCache配置
  • OpCache statistics OpCache统计
  • OpCache reset OpCache重置
  • Cached scripts overview 缓存脚本概述
  • Cached scripts invalidation 缓存脚本失效
  • Automatic refresh 自动刷新

Screenshot: 截图:

opcache  - 桂 - 概述

URL: https://github.com/amnuts/opcache-gui 网址: https//github.com/amnuts/opcache-gui

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. 由于OPcache旨在取代APC模块,因此无法在PHP中并行运行它们。 This is fine for caching PHP opcode as neither affects how you write code. 这对于缓存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 APCu or Yac which 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. 您将需要使用另一个库,例如APCuYac ,它们都将数据存储在共享的PHP内存中,或者切换到使用memcached之类的东西,它将数据存储在内存中,与PHP分开。

Also, OPcache has no equivalent of the upload progress meter present in APC. 此外,OPcache没有相当于APC中存在的上传进度表。 Instead you should use the Session Upload Progress . 相反,您应该使用会话上载进度

Settings for OPcache OPcache的设置

The documentation for OPcache can be found here with all of the configuration options listed here . 对于OPcache的文档,可以发现这里所有列出的配置选项在这里 The recommended settings are: 建议的设置是:

; 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. 如果禁用,则从代码中删除所有PHPDoc注释以减小优化代码的大小。 Disabling "Doc Comments" may break some existing applications and frameworks (eg Doctrine, ZF2, PHPUnit) 禁用“Doc Comments”可能会破坏一些现有的应用程序和框架(例如Doctrine,ZF2,PHPUnit)

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)

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)

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. 我已经制作了一个opcache黑名单文件来禁用除框架之外的所有内容的缓存,这些内容都是静态的,不需要更改功能。 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 让所有缓存都会在请求总数上节省一秒25546 milliseconds

This significantly expands the amount of data/requests I can handle per second without the server even breaking a sweat. 这显着扩展了我每秒可以处理的数据/请求数量,而服务器甚至不会出汗。

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。

I encountered this when setting up moodle. 我在设置moodle时遇到了这个问题。 I added the following lines in the php.ini file. 我在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

intl -> http://php.net/manual/en/book.intl.php 国际 - > http://php.net/manual/en/book.intl.php

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

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