search
HomeBackend DevelopmentPHP Tutorial[Modern PHP] 第二章 新特性之6 Zend OPcache

[Modern PHP] 第二章 新特性之六 Zend OPcache

Zend OPcache


字节码缓存技术对于PHP来说并不新鲜。我们很早就有了Alternative PHP Cache(APC)、eAccelerator、ionCube和XCache这些独立的扩展,它们都可以作为我们的可选方案。但是在PHP的每个核心发布版本中都没有它们的身影。直到现在PHP 5.5.0后,PHP才有了自己内置的字节码缓存:Zend OPcache。


首先,让我来解释一下什么是字节码以及它的重要性。PHP是一种解释语言。当PHP解释器执行一个PHP脚本时,解释器解析PHP的脚本代码,将PHP代码编译成一组Zend Opcodes(机器码指令),最终执行这些字节码。PHP文件在每次请求时都会重复上面的步骤。这样做未免太浪费了,尤其是每次HTTP请求时PHP脚本都要一次又一次的执行解析、编译和执行。如果我们有办法能够缓存住这些编译好的字节码就可以缩短应用程序的响应时间,并且能够减轻系统资源的压力。你真幸运。


字节码缓存能够存储编译后的PHP字节码。这意味着每次请求时PHP解释器不再需要读取、解析和编译PHP代码,而是可以直接从内存中读取编译后的字节码并执行。这大大节省了时间,极大的提升了应用程序的性能。


开启Zend OPcache


Zend OPcache默认是不启用的,你需要在安装编译PHP时明确的开启Zend OPcache才行。


如果你使用的是虚拟主机,请确保你选择的是一家能够提供PHP 5.5.0及以上版本并且开启Zend OPcache的优秀服务商。


如果你自己编译PHP(假设你使用的是VPS或者服务器托管),你必须在PHP的 ./configure命令后添加一个参数

--enable-opcache

在PHP编译完成后,你还需要在phpini文件中指定Zend OPcache扩展的路径,参照下面的示例:

zend_extension=/path/to/opcache.so


在PHP编译成功后会立刻显示Zend OPcache扩展的文件路径的。如果你忘了像我说的这么做,你也可以执行下面的命令来获取PHP所有扩展存放的路径地址:

php-config --extension-dir


如果你在使用无与伦比的Derick Rethans开发流行调试工具Xdebug,在php.ini文件中,Zend OPcache扩展必须在Xdebug扩展之前加载。


在你更新了php.ini文件并且重启PHP进程后就可以使用了。如果需要确认Zend OPcache是否正确安装,可以创建一个PHP文件包含下面的内容:

<?phpphpinfo ();

在浏览器中查看这个PHP文件,并且下拉滚动条直到看到Zend OPcache扩展那一段信息,如图2-2所示。如果你没有看到这段信息,就表示Zend OPcache并没有在运行。


图 2-2 Zend OPcache INI设置


配置Zend OPcache


在Zend OPcache启用的情况下,你可以在php.ini配置文件中配置Zend OPcache。下面是我喜欢使用的OPcache设置:

opcache.validate_timestamps =1 // 在生产环境中使用"0"

opcache.revalidate_freq =0

opcache.memory_consumption =64

opcache.interned_strings_buffer =16

opcache.max_accelerated_files =4000

opcache.fast_shutdown =1


想了解更多关于这些Zend OPcache设置的信息可以阅读第八章。在PHP.net上可以获取到完整的设置列表。


使用Zend OPcache


这部分内容很简单,因为Zend OPcache一旦启用就会自动工作。Zend OPcache会自动将编译后的PHP字节码缓存到内存中并自动执行混存后的字节码。


当INI参数 opcache.validate_timestamps设置为false(0)时需要格外小心。这种情况下,Zend OPcache不会检测你的PHP脚本的改动,因此你必须在改动了PHP文件后手动的去清除Zend OPcaches的字节码缓存才行。这个设置对线上的产品服务器有很大的帮助,但是会给开发带来很大的不便。你可以在开发时使用下面的php.ini的配置设置来启用自动的文件检测:

opcache.validate_timestamps = 1

opcache.revalidate_freq = 0

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),