[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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
