博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
了解OpCache
阅读量:2505 次
发布时间:2019-05-11

本文共 11280 字,大约阅读时间需要 37 分钟。

PHP in version 5.5 comes with a caching engine built-in – – which stores precompiled script bytecode in the memory. If you’re familiar with APC or Xcache, you will already know how such engines work. As each PHP script is being compiled at runtime, a part of the execution time gets used for transforming the human readable code into code that can be understood by the machine. A bytecode cache engine like OpCache, APC or Xcache does it only once – during the first execution of a specific PHP file. Then the precompiled script is being stored in memory, which should lead to performance boosts in your PHP applications.

5.5版PHP带有内置的缓存引擎– ,该缓存引擎将预编译的脚本字节码存储在内存中。 如果您熟悉APC或Xcache,您将已经知道这些引擎如何工作。 在运行时编译每个PHP脚本时,将执行时间的一部分用于将人类可读的代码转换为机器可以理解的代码。 字节代码缓存引擎(例如OpCache,APC或Xcache)仅在一次特定PHP文件执行期间执行一次。 然后,将预编译的脚本存储在内存中,这将导致PHP应用程序的性能提高。

Over the web you will easily find a lot of tutorials covering all the OpCache installation and configuration steps (it is enabled by default on 5.5, but can be installed as an extension on older versions). Read the article below to find the answers to some of the typical questions regarding different practical aspects of working with this particular cache engine.

在Web上,您可以轻松找到许多教程,涵盖所有OpCache安装和配置步骤(默认情况下在5.5上启用此功能,但可以将其作为扩展安装在旧版本上)。 阅读下面的文章,以找到有关使用此特定缓存引擎的不同实际方面的一些典型问题的答案。

1. OpCache是​​否值得安装? 我可以期待什么速度提升? (1. Is OpCache worth installing at all? What speed boost can I expect?)

Of course, it depends. If your server manages to handle the incoming traffic and keep low response times, you probably won’t feel the need to work on performance. But on a bigger website with a lot of traffic each little optimization step may count. Implementing OpCache may allow you to handle more requests per second and return the response quicker than without a bytecode caching engine. As OpCache is quite easy to install and configure, you won’t spend too much time on setting everything up.

当然,这取决于。 如果您的服务器设法处理传入流量并保持较低的响应时间,则您可能不会觉得需要提高性能。 但是在一个拥有大量流量的更大的网站上,每个小的优化步骤可能都很重要。 与没有字节码缓存引擎相比,实现OpCache可以使您每秒处理更多请求并更快地返回响应。 由于OpCache易于安装和配置,因此您无需花费太多时间进行所有设置。

If you’re looking for some OpCache performance tests, you should certainly read the on implementing this cache engine. In their tests the installation of OpCache reduced the mean response time of a website by 14%. The reduction of response times of different actions of a web application varied from 6% to as much as 74%. As explained in the article, different code parts may benefit more or less from implementing a bytecode caching engine. I encourage you to read the article in detail and check where you can get the highest performance boost.

如果您正在寻找一些OpCache性能测试,则应该阅读有关实现此缓存引擎的 。 在他们的测试中,OpCache的安装使网站的平均响应时间减少了14%。 Web应用程序不同动作的响应时间减少了6%至多达74%。 如本文所述,不同的代码部分可能会或多或少地受益于实现字节码缓存引擎。 我鼓励您详细阅读本文,并检查在哪里可以获得最高的性能提升。

If you’re looking for some other resources regarding the OpCache performance, check the examples on and . Both tests reported about 50% decrease in response time after installing OpCache.

如果您正在寻找关于OpCache性能的一些其他资源,请检查例子和 。 两项测试均表明安装OpCache后响应时间减少了约50%。

2.我已经在使用APC缓存了。 我应该迁移到OpCache吗? (2. I already use APC cache. Should I migrate to OpCache?)

I think you should. There are a couple of advantages that can make OpCache a better solution than APC.

我想你应该。 有许多优点可以使OpCache成为比APC更好的解决方案。

First of all, the APC cache won’t work with the newest PHP versions. It doesn’t support PHP 5.5 at all. It’s not advised to install it on PHP 5.4 either, as it has been reported that such configuration can lead to execution errors that break the whole application. Just check the on PHP accelerators or to get some details.

首先,APC缓存不适用于最新PHP版本。 它根本不支持PHP 5.5。 不建议将其安装在PHP 5.4上,因为据报道,这种配置会导致执行错误,从而破坏整个应用程序。 只需查看有关PHP加速器的或即可获得一些详细信息。

OpCache, however, is bundled with PHP 5.5 so it will definitely work with this PHP version. As written in the , it may also be installed with older PHP versions – from 5.2 to 5.4. Generally, OpCache is more closely bound to PHP itself than other bytecode cache engines – which may result in more frequent updates and fewer errors (read for details).

但是,OpCache与PHP 5.5捆绑在一起,因此肯定可以在此PHP版本中使用。 如中所述,它也可能安装有较旧PHP版本-从5.2到5.4。 通常,与其他字节码缓存引擎相比,OpCache与PHP本身的绑定更为紧密-这可能导致更新更加频繁且错误更少(请参阅以获取详细信息)。

The only important thing to remember when migrating from APC to OpCache is the fact that the latter doesn’t work as a data caching engine. If you have already implemented APC, you may be using its and functions which serve as an interface to a data caching service. OpCache is only a bytecode caching engine so it won’t offer a similar functionality. If you’re planning to switch from APC to OpCache, remember to consider this limitation. Keep in mind that if you want to keep APC userland functionality, there’s the project (many thanks to Oscar Merina for pointing it out in the comments).

从APC迁移到OpCache时,唯一要记住的重要事实是,后者不能用作数据缓存引擎。 如果您已经实现了APC,则可能正在使用其和函数作为数据缓存服务的接口。 OpCache只是一个字节码缓存引擎,因此不会提供类似的功能。 如果您打算从APC切换到OpCache,请记住考虑此限制。 请记住,如果要保留APC用户界面功能,则有一个项目(非常感谢Oscar Merina在评论中指出了这一点)。

3.如何检查OpCache是​​否确实在缓存我的文件? (3. How to check if OpCache is actually caching my files?)

If you have already installed and configured OpCache, you may find it important to control which PHP files are actually being cached. The whole cache engine works in the background and is transparent to a visitor or a web developer. In order to check its status, you may use one of the two functions that provide such information: and . Fortunately, there is a couple of prepared scrips that fetch all the OpCache configuration and status data and display it in a friendly way. You don’t need to write any code by yourself, just pick up one of tools from these below: – , – by Rasmus Lerdorf, – by Pieter Hordijk, – by Andrew Collington.

如果已经安装并配置了OpCache,则可能很重要的一点是,控制实际上要缓存哪些PHP文件。 整个缓存引擎在后台运行,并且对访问者或Web开发人员是透明的。 为了检查其状态,可以使用提供以下信息的两个函数之一: 和 。 幸运的是,有一些准备好的脚本可以读取所有OpCache配置和状态数据并以友好的方式显示它们。 你并不需要编写自己的任何代码,只需拿起工具,一个从下面这些: - , - 由拉斯姆斯·勒多夫, - 由彼得Hordijk, - 由安德鲁Collington。

In my projects I use the opcache-gui script which has all the functionality I need. To check if the caching engine works properly, just look at the percentages at the “Overview” tab at the opcache-gui page. If the memory usage and hit rate values are greater than zero, it means that the OpCache is caching the PHP code and the cached files are being used to handle the requests. To see a list of specific PHP files that are being cached, just navigate to the tab “File usage”. Look through the file list to make sure that the files inside your projects got cached.

在我的项目中,我使用opcache-gui脚本,该脚本具有我需要的所有功能。 要检查缓存引擎是否正常运行,只需查看opcache-gui页面“概述”选项卡上的百分比。 如果内存使用率和命中率值大于零,则表示OpCache正在缓存PHP代码,并且已使用缓存的文件来处理请求。 要查看正在缓存的特定PHP文件的列表,只需导航至选项卡“文件使用情况”。 浏览文件列表以确保项目内的文件得到缓存。

alt

4.我应该设置任何特定于框架的配置吗? (4. Is there any framework-specific config that I should set?)

Basically, the OpCache engine should be transparent to you as a code writer. It means that you can put any code you want in your application (which works with the PHP version you’re using) and OpCache should handle it properly. But it will work only if the OpCache configuration is set in a correct way. A wrong cache engine configuration can break your entire website.

基本上,OpCache引擎对您作为代码编写者而言应该是透明的。 这意味着您可以将所需的任何代码放入应用程序中(该代码可与您使用PHP版本一起使用),OpCache可以正确处理它。 但是,只有在正确设置OpCache配置后,它才能起作用。 错误的缓存引擎配置可能会破坏您的整个网站。

First of all, probably in each typical project you will have to ensure that the option is set to true. Enabling this setting means that the OpCache engine will look at the full file paths to distinguish between files with the same names. Setting it to false will lead to collisions between files with the same base name.

首先,可能在每个典型项目中,您都必须确保将选项设置为true 。 启用此设置意味着OpCache引擎将查看完整的文件路径,以区分具有相同名称的文件。 将其设置为false将导致具有相同基本名称的文件之间发生冲突。

There is also a setting that is important in tools and frameworks that make use of annotations. If you use Doctrine, Zend Framework 2 or PHP Unit, remember to set the and settings to true. In result, the documentation comments from your files will also be included in the precompiled code generated by OpCache. This setting will allow you to work with annotations without any disruptions.

在使用注释的工具和框架中,还有一个重要的设置。 如果使用Doctrine,Zend Framework 2或PHP Unit,请记住将和设置设置为true 。 结果,来自文件的文档注释也将包含在OpCache生成的预编译代码中。 此设置将允许您使用注释而不会造成任何干扰。

If your project is based on a specific framework or a web application, it’s always a good idea to check the documentation for any guidelines regarding the OpCache configuration. For example, there is a sample configuration for Moodle .

如果您的项目基于特定的框架或Web应用程序,则最好查看文档中有关OpCache配置的任何准则。 例如,存在一种用于Moodle的一个示例配置 。

5.我将应用程序配置保存在一个PHP文件中。 我可以防止它被缓存吗? (5. I keep my app config in a PHP file. Can I prevent it from being cached?)

If your project contains files that are being changed more frequently than the others, you can exclude them from caching. It may be especially handy when dealing with PHP files that contain configuration directives for your website. If you keep them from being cached, you can be sure that each change put in such a file will be visible in your application immediately.

如果您的项目包含比其他文件更频繁地更改的文件,则可以将它们从缓存中排除。 在处理包含网站配置指令PHP文件时,它可能特别方便。 如果您不缓存它们,则可以确保对此类文件进行的每个更改都将立即在您的应用程序中可见。

OpCache allows you to specify a blacklist file that contains all the paths that won’t be processed by the caching engine. After setting the directive, just put the proper file list in the path you’ve specified. The contains some examples on how to exclude specific files from cache.

OpCache允许您指定一个黑名单文件,该文件包含缓存引擎不会处理的所有路径。 设置指令后,只需将正确的文件列表放在指定的路径中即可。 包含一些有关如何从缓存中排除特定文件的示例。

6.如何在启用OpCache的单个服务器上同时运行开发和生产环境? (6. How can I run both a development and a production environment on a single server where OpCache is enabled?)

If your server runs several apps, you may be willing to use OpCache only in some of them. Development and testing sites are examples of such environments where having bytecode caching enabled is not preferable and can cause more harm than good.

如果服务器运行多个应用程序,则您可能只愿意在其中一些应用程序中使用OpCache。 开发和测试站点就是此类环境的示例,在这些环境中,启用字节码缓存不是可取的,弊大于利。

Fortunately, it is possible to have OpCache enabled in one project and disabled in another, all on a single server. To do so, first you need to enable the OpCache at the global level by setting the directive to true in your php.ini file. Then, if you don’t want to use bytecode caching in one of your projects, just disable it by setting the same directive to false using the . So first you need to enable the caching globally and then you can disable it in some projects “locally”. You can’t do the opposite – enabling caching using is not allowed.

幸运的是,有可能在一个项目中启用OpCache并在另一个项目中禁用OpCache,所有这些都在一台服务器上。 为此,首先需要通过在php.ini文件指令设置为true在全局级别启用OpCache。 然后,如果您不想在您的一个项目中使用字节码缓存,只需使用将相同的伪指令设置为false将其禁用。 因此,首先需要全局启用缓存,然后可以在某些项目中“本地”禁用缓存。 您不能做相反的事情–不允许使用缓存。

Setting opcache.enable to false in a top file in your project will disable the caching while keeping it on elsewhere.

在项目的顶层文件中将opcache.enable设置为false将禁用缓存,同时将其保留在其他位置。

摘要 (Summary)

I hope this set of common questions and answers gave you some practical information on how to use OpCache in a PHP application. If you have other questions or comments regarding the subject, feel free to post them below. You can also contact me through .

我希望这组常见问题和解答为您提供了一些有关如何在PHP应用程序中使用OpCache的实用信息。 如果您对此主题还有其他疑问或意见,请随时在下面发布。 您也可以通过与我联系。

翻译自:

转载地址:http://fargb.baihongyu.com/

你可能感兴趣的文章
[转]A星寻路算法
查看>>
centos 安装 rabbitMQ
查看>>
POJ 1679 The Unique MST(次小生成树)
查看>>
OpenStack的容器服务体验
查看>>
[ios基础]IOS应用程序的生命周期问题
查看>>
python 之tornado 入门
查看>>
U3D的控制
查看>>
js浮点乘除法运算不精确bug
查看>>
BZOJ1443: [JSOI2009]游戏Game
查看>>
【BZOJ 4059】 (分治暴力|扫描线+线段树)
查看>>
BZOJ 1066 蜥蜴(网络流)
查看>>
提高批量插入数据的方法
查看>>
Linux重启Mysql命令
查看>>
前端模块化:RequireJS(转)
查看>>
linux 内核的优化
查看>>
Spark笔记之DataFrameNaFunctions
查看>>
Oracle 时间函数 (转)
查看>>
近端梯度算法(Proximal Gradient Descent)
查看>>
DRM-内容数据版权加密保护技术学习(中):License预发放实现 (转)
查看>>
TCP与UDP协议
查看>>