Home Backend Development PHP Tutorial [Modern PHP] 第二章 新特性之6 Zend OPcache

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

Jun 13, 2016 pm 12:23 PM
ini opcache php zend

[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 ();
Copy after login

在浏览器中查看这个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 of this Website
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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles