opcache 缓存_缓存帽子戏法:Zend Opcache,Etags和查询缓存

opcache 缓存

In this article, we will be looking at some of the common caching techniques in PHP: Opcache, Expires Headers and Query Caching in MySQL. We’ll look at additional approaches in part 2.

在本文中,我们将研究PHP中的一些常见缓​​存技术:Opcache,Expires Headers和MySQL中的查询缓存。 我们将在第2部分中介绍其他方法。

Abstract image with cache text

PHP的请求生命周期 (PHP’s Request Lifecycle)

Before we move on, it’s important to understand PHP’s request lifecycle. What happens behind the scenes when you access a PHP file from the browser?

在继续之前,了解PHP的请求生命周期很重要。 从浏览器访问PHP文件时,幕后会发生什么?

  1. A file is fetched from the file system – it doesn’t really matter if it has changed since the last request.

    从文件系统中提取文件–自上次请求以来是否已更改,这并不重要。
  2. Lexical analysis – the human-readable code is converted into something (tokens) that the parser can understand.

    词法分析–将人类可读的代码转换为解析器可以理解的内容(标记)。
  3. Parsing – the machine-readable strings are analyzed for potential errors. This is like grammar-checking.

    解析–分析机器可读的字符串是否存在潜在错误。 这就像语法检查。
  4. Opcode Creation – the tokens that we got from step 2 are converted into machine executable code.

    创建操作码–我们将从步骤2中获得的令牌转换为机器可执行代码。
  5. Machine code execution – machine code is processed and then executed.

    机器代码执行–处理机器代码,然后执行。

For a more in-depth explanation, check out this article on how PHP echos a Hello World.

有关更深入的说明,请查看有关PHP如何回声Hello World的本文

All of those steps happen really quickly, and yet there is a lot of time and resources wasted on every request. That is because for every request for each PHP file on the server, the server has to go through all of them.

所有这些步骤都非常Swift地发生,但是在每个请求上浪费了大量时间和资源。 那是因为对于服务器上每个PHP文件的每个请求,服务器都必须遍历所有这些文件。

The caching techniques that we will be going through in these two articles help bypass steps 2, 3 and 4. That means less time and fewer resources wasted, leading to faster page load times for the user.

我们将在这两篇文章中介绍的缓存技术有助于绕过步骤2、3和4。这意味着更少的时间和更少的资源浪费,从而为用户带来了更快的页面加载时间。

Zend Opcache (Zend Opcache)

The first caching tool we’re going to look at is Zend Opcache.

我们要看的第一个缓存工具是Zend Opcache。

It comes pre-installed with PHP 5.5 and newer. If running php --version from your terminal gives you something like the following, you’re good to go:

它已预装PHP 5.5及更高版本。 如果从终端运行php --version可以得到如下所示的信息,那您就可以了:

Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

Alternatively, you can create a new PHP file, call phpinfo(), then access the file from your browser. Look for ‘Zend Opcache’. It should give you something similar to the following:

另外,您可以创建一个新PHP文件,调用phpinfo() ,然后从浏览器访问该文件。 寻找“ Zend Opcache”。 它应该给您类似于以下内容:

zend opcache

激活 (Activation)

Once you have confirmed that Zend Opcache is installed, you can enable it by opening your php.ini file, then searching for the term ‘opcache’, uncommenting opcache.enable=0 and setting its value to 1:

确认已安装Zend Opcache后,可以通过打开php.ini文件,然后搜索术语“ opcache”,取消注释opcache.enable=0并将其值设置为1来启用它:

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

Don’t forget to restart PHP to activate the changes.

不要忘记重新启动PHP以激活更改。

If you are using a PHP version below 5.5, you can still take advantage of Zend Opcache by installing it via PECL. You can do so by executing the following command:

如果您使用PHP版本低于5.5,则仍然可以通过PECL安装Zend Opcache。 您可以通过执行以下命令来这样做:

pecl install zendopcache-beta

组态 (Configuration)

You can configure Zend Opcache by editing the opcache.ini file under /etc/php5/mods-available/ on Debian-based distributions. By default, the configuration file contains the following:

您可以通过在基于Debian的发行版的/etc/php5/mods-available/下编辑opcache.ini文件来配置Zend Opcache。 默认情况下,配置文件包含以下内容:

; configuration for php ZendOpcache module
; priority=05
zend_extension=opcache.so

Here are the most important configuration options you can tweak to your liking:

您可以根据自己的喜好调整以下最重要的配置选项:

  • opcache.memory_consumption – the size of the shared memory storage used. This is expressed in megabytes and defaults to 64. You can go higher, depending on how much power your server packs and how much memory you think your application is going to need.

    opcache.memory_consumption –使用的共享内存存储的大小。 它以兆字节表示,默认为64。您可以提高到更高的水平,具体取决于服务器包装的电量以及您认为应用程序需要的内存量。

  • opcache.interned_strings_buffer – The amount of memory used to store interned strings, also in megabytes. An interned string is an approach in which only a single copy of each unique string is stored in memory.

    opcache.interned_strings_buffer –用于存储实习字符串的内存量,也以兆字节为单位。 插入字符串是一种方法,其中每个唯一字符串的单个副本仅存储在内存中。

  • opcache.max_accelerated_files – The maximum number of files that can be accelerated by opcache. Only numbers between 200 and 100000 are allowed.

    opcache.max_accelerated_filesopcache可以加速的最大文件数。 仅允许使用200到100000之间的数字。

  • opcache.revalidate_freq – The number of seconds after which to check PHP files for changes. Giving it a value of 1 means checking for changes once per second, but only once per request. 0 means it will always check for changes. This is the optimal setting for development environments.

    opcache.revalidate_freq –检查PHP文件是否更改的秒数。 将该值设置为1意味着每秒检查一次更改,但每个请求仅检查一次。 0表示它将始终检查更改。 这是开发环境的最佳设置。

  • opcache.max_file_size – Allows exclusion of large files from being cached. By default, it has a value of 0 which means that all files are cached. The value for this option is expressed in bytes.

    opcache.max_file_size –允许从缓存中排除大文件。 默认情况下,它的值为0 ,表示所有文件都已缓存。 此选项的值以字节表示。

  • opcache.fast_shutdown – When enabled, the deconstructors get a speed boost at the end of each request. This means that the subsequent request becomes faster. By default, it’s set to 0. Setting it to 1 will enable it.

    opcache.fast_shutdown –启用后,在每次请求结束时,解构函数都会提高速度。 这意味着后续请求变得更快。 默认情况下,它设置为0 。 将其设置为1将启用它。

More configuration options are available here: OPCache Runtime Configuration.

此处提供更多配置选项: OPCache Runtime Configuration

To check if opcache is working properly, you can install opcache-gui by executing the following command from the terminal

要检查opcache是​​否正常运行,可以通过从终端执行以下命令来安装opcache-gui

composer require amnuts/opcache-gui

Once installed, you can copy the index.php file from the vendor/amnuts/opcache-gui directory to the root of the desired project directory and access it from your browser. It will then show you something similar to the following:

安装后,您可以将index.php文件从vendor/amnuts/opcache-gui目录复制到所需项目目录的根目录,然后从浏览器访问它。 然后它将向您显示类似于以下内容:

alt

It will show you the memory usage, hit rate, and the configuration options that you have set. You can also reset the cache or invalidate files manually.

它将显示内存使用率,命中率以及您设置的配置选项。 您还可以手动重置缓存或使文件无效。

过期标题 (Expires Headers)

We can also use Apache for caching (instructions for Nginx at the end of this section). With Apache, we can mainly cache static files such as stylesheets, script files, images and other media files.

我们还可以使用Apache进行缓存(本节末尾的Nginx指令)。 使用Apache,我们主要可以缓存静态文件,例如样式表,脚本文件,图像和其他媒体文件。

By default, Apache uses etags. These are hashes of a file that are sent in the response header. The browser doesn’t make a request for that file again if the etag is the same, and uses the cached version instead until the default value for the expires header becomes greater than or equal to the time on the client’s computer. But when you make a change to a file, the etag changes and the browser will have to request the file again from Apache. This is all well and good, but we can take it further by specifying an expiration time. That way we can have the browser hold on to the cached version of the file for a longer period.

默认情况下,Apache使用etags。 这些是在响应标头中发送的文件的哈希。 如果etag相同,浏览器不会再次请求该文件,而是使用缓存的版本,直到expires标头的默认值大于或等于客户端计算机上的时间为止。 但是,当您对文件进行更改时,etag也会更改,浏览器将不得不再次从Apache请求该文件。 这一切都很好,但是我们可以通过指定到期时间来进一步完善它。 这样,我们就可以让浏览器将文件的缓存版本保留更长的时间。

Specifying an expires header via Apache tells browsers to cache static resources for a specific period of time.

通过Apache指定expires标头可以告诉浏览器在特定时间段内缓存静态资源。

To enable the use of this functionality you must first enable the expires module. You can do that by executing the following commands from the terminal:

要启用此功能,您必须首先启用expires模块。 您可以通过从终端执行以下命令来做到这一点:

sudo a2enmod expires
sudo service apache2 restart

The first line enables the expires module and the second line restarts Apache in order for changes to take effect.

第一行启用expires模块,第二行重新启动Apache,以使更改生效。

Once that’s done you can use the expires headers from your virtual server’s configuration file, usually residing in etc/apache/sites-available or something similar – check your installation’s documentation.

完成此操作后,您可以使用虚拟服务器的配置文件中的expires标头(通常位于etc/apache/sites-available或类似的目录中)–检查安装文档。

Open up the server config file and look for the Directory directive to find the directory corresponding to the project you’re setting etags up for. There, you can start using the expires module:

打开服务器配置文件,然后查找Directory指令以找到与您要为其设置标签的项目相对应的目录。 在那里,您可以开始使用expires模块:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 day"
ExpiresByType image/png "access plus 10 days"
ExpiresByType text/css "access plus 25 days"
</IfModule>

The first and last line are a wrapper where we check if the mod_expires module is already enabled. In the second line, we specify that we want to start using the module. The third to fifth line is where we set the rules. Here’s a description of what each option does:

第一行和最后一行是包装器,我们在其中检查是否已启用mod_expires模块。 在第二行中,我们指定我们要开始使用该模块。 第三到第五行是我们设置规则的地方。 以下是每个选项的作用说明:

  • ExpiresDefault – allows you to specify the default expire rule for all static files. This accepts the maximum timeframe during which you want the browser to hold on to the file.

    ExpiresDefault –允许您为所有静态文件指定默认的过期规则。 这将接受您希望浏览器保留文件的最大时间范围。

  • ExpiresByType – allows you to specify the expire time based on file type. This accepts the mime type as the first argument and the expire time as the second.

    ExpiresByType –允许您根据文件类型指定过期时间。 这将mime类型作为第一个参数,将到期时间作为第二个参数。

You can now check using the network tab in the Chrome Developer Tools that it has indeed changed the value for the Expires header based on the setting that you specified. The screenshot below is that of a stylesheet. The page is viewed on Feb 1, 2015 so we just add 25 days to that and get the expiration date:

现在,您可以使用Chrome开发者工具中的网络标签检查其是否确实根据您指定的设置更改了Expires标头的值。 下面的屏幕截图是样式表的屏幕截图。 该页面是在2015年2月1日开始查看的,因此我们只添加了25天并获得了到期日期:

expires header

To configure expiration headers for Nginx, see this excellent post.

要为Nginx配置过期标头,请参阅这篇优秀的文章

MySQL查询缓存 (MySQL Query Caching)

For web applications which mostly read data from the database, you can take advantage of query caching. This puts the parsed version of an SQL query and its corresponding result set into the cache which in turn makes subsequent requests for pages using the same set of queries retrieve the results faster. This is because your application no longer has to go to the database to parse the query and fetch the results.

对于主要从数据库读取数据的Web应用程序,您可以利用查询缓存。 这会将已解析SQL查询版本及其对应的结果集放入缓存,从而使使用同一组查询的后续页面请求可以更快地检索结果。 这是因为您的应用程序不再需要去数据库来解析查询并获取结果。

For MySQL versions below 5.6.8, query caching is enabled by default. You can check which version you have installed by executing the following command from your terminal:

对于低于5.6.8MySQL版本,默认情况下启用查询缓存。 您可以通过从终端执行以下命令来检查已安装的版本:

mysql --version

mysql  Ver 14.14 Distrib 5.5.41, for debian-linux-gnu (x86_64) using readline 6.3

配置查询缓存 (Configuring Query Cache)

In this case the version is 5.5.41 so I know that query caching is enabled. If you have version 5.6.8 and above you need to enable it. You can do so by editing the configuration file. On Ubuntu, it’s in /etc/mysql/my.cnf.

在这种情况下,版本为5.5.41,因此我知道启用了查询缓存。 如果您具有5.6.8及更高版本,则需要启用它。 您可以通过编辑配置文件来实现。 在Ubuntu上,它位于/etc/mysql/my.cnf

Once you have the file open, try to find the string # * Query Cache Configuration. If it exists, put your configuration below it. If not, then navigate to the bottom of the file and add it:

打开文件后,尝试查找字符串# * Query Cache Configuration 。 如果存在,请将您的配置放在下面。 如果不是,请导航到文件底部并添加它:

# * Query Cache Configuration
query_cache_type             = ON
query_cache_min_res_unit     = 4096
query_cache_limit            = 1M
query_cache_size             = 16M
query_cache_wlock_invalidate = OFF

Note that the values in the sample above are the default configuration on MySQL version 5.5, so you’ll have to change them based on your needs and the capacity of your server. The general rule for tweaking the configuration is to not go with really high values if your database is updated frequently, as it’s going to take a longer time to invalidate the items in the cache once they are updated. To give you an idea of how much you can set for each item, here’s a brief description of each. Note that when an integer value is used without adding the unit such as M (for MB), then it is expressed in bytes:

请注意,上面示例中的值是MySQL 5.5版的默认配置,因此您必须根据需要和服务器容量进行更改。 调整配置的一般规则是,如果数据库经常更新,则不要使用很高的值,因为一旦更新了缓存中的项目,将需要更长的时间来使它们无效。 为了让您大致了解每个项目可以设置的数量,以下是每个项目的简要说明。 请注意,当使用整数值而未添加单位(例如M(对于MB))时,则以字节表示:

  • query_cache_type – allows you to specify whether to enable query caching or not. You can set the value to either ON or OFF.

    query_cache_type –允许您指定是否启用查询缓存。 您可以将值设置为ONOFF

  • query_cache_limit – the maximum size of a result set that can be cached per query. Note that when a specific query has a result set exceeding the value specified in this option, it wouldn’t be included in the query cache at all.

    query_cache_limit –每个查询可以缓存的结果集的最大大小。 请注意,当特定查询的结果集超过此选项中指定的值时,它根本不会包含在查询缓存中。

  • query_cache_min_res_unit – the minimum amount of memory that can be allocated to store a query.

    query_cache_min_res_unit –可以分配用于存储查询的最小内存量。

  • query_cache_size – the total size of the query cache.

    query_cache_size –查询缓存的总大小。

  • query_cache_wlock_invalidate – allows you to specify whether to invalidate the query cache when a specific table is locked for writing. You can set the value to either ON or OFF.

    query_cache_wlock_invalidate –允许您指定在锁定特定表以进行写入时是否使查询缓存无效。 您可以将值设置为ONOFF

Once you’re done tweaking your query cache configuration, you can now check if it is indeed functioning by executing the following query:

调整完查询缓存配置后,现在可以通过执行以下查询来检查其是否确实起作用:

show variables like 'query%';

This will then return results similar to the following:

然后,将返回类似于以下内容的结果:

is query caching enabled?

Note that the results returned for all integer values are expressed in bytes.

请注意,所有整数值返回的结果均以字节表示。

测试查询缓存 (Testing Query Cache)

Execute a SELECT query in one of the databases that you currently have in the machine you’re working on.

在正在使用的计算机上当前拥有的数据库之一中执行SELECT查询。

Next, execute the following query:

接下来,执行以下查询:

SHOW STATUS LIKE  "qcache%";

This shows you something similar to the following:

这向您显示类似于以下内容:

query cache stats

Take note of the values that you get and then execute the same SELECT query that you executed earlier. If you’re using phpmyadmin, you can just click on the refresh link under the query box.

记下您获得的值,然后执行与之前执行的相同的SELECT查询。 如果您使用的是phpmyadmin,则只需单击查询框下的刷新链接即可。

You can also do the same for the SHOW STATUS query. It should now have incremented the values for the Qcache_free_memory, Qcache_hits, Qcache_inserts, Qcache_not_cached, Qcache_queries_in_cache, and Qcache_total_blocks. If those values have incremented then query caching is working.

您也可以对SHOW STATUS查询执行相同的操作。 现在,它应该已经增加了Qcache_free_memoryQcache_hitsQcache_insertsQcache_not_cachedQcache_queries_in_cacheQcache_total_blocks 。 如果这些值增加,则查询缓存将正常工作。

查询缓存陷阱 (Query Cache Gotchas)

Here are some things to remember when using query cache:

使用查询缓存时,请记住以下几点:

  • It only works with SELECT queries. SHOW queries aren’t cached since they are usually used for getting configuration values or general information about the server itself.

    它仅适用于SELECT查询。 SHOW查询不会被缓存,因为它们通常用于获取配置值或有关服务器本身的常规信息。

  • Queries should be exactly the same in order for the cache to work. This means that if you update even a single character of the same query, it would be considered a different query, which means it won’t hit the cache.

    为了使缓存正常工作,查询应该完全相同。 这意味着,即使您更新同一查询的单个字符,也将被视为不同的查询,这意味着它不会命中缓存。

  • Only deterministic queries can take advantage of the cache. This means that functions like RAND() or CONNECTION_ID() or any other function which changes the results on each subsequent execution of the same query cannot take advantage of the query cache.

    只有确定性查询才能利用缓存。 这意味着诸如RAND()CONNECTION_ID()函数或任何其他在随后执行同一查询时更改结果的函数均无法利用查询缓存。

  • Table updates such as when inserting new rows, updating the table schema, or updating a row automatically invalidate the query cache.

    表更新(例如在插入新行,更新表架构或更新行时)会自动使查询缓存无效。

结论 (Conclusion)

In this part, we looked at some common caching techniques to use with PHP and MySQL. In the followup, we’ll take a look at some other software which can further speed up our apps – Varnish, Memcached and some PHP caching libraries. Stay tuned!

在这一部分中,我们研究了与PHP和MySQL一起使用的一些常见缓​​存技术。 在后续文章中,我们将介绍其他一些可以进一步加快应用程序速度的软件-Varnish,Memcached和一些PHP缓存库。 敬请关注!

翻译自: https://www.sitepoint.com/caching-hat-trick-zend-opcache-etags-and-query-caching/

opcache 缓存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值