选择一种PHP缓存技术:将输出缓存到文件中,而不是操作码缓存 [英] Choosing a PHP caching technique: output caching into files vs. opcode caching

查看:130
本文介绍了选择一种PHP缓存技术:将输出缓存到文件中,而不是操作码缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过两种PHP代码缓存技术:

I've heard of two caching techniques for the PHP code:

  1. PHP脚本生成输出时,会将其存储到本地文件中.再次调用该脚本时,它检查先前输出的文件是否存在,如果为true,则返回该文件的内容.这主要是通过在输出缓冲区"中播放来完成的. 这篇文章中对此进行了描述.

  1. When a PHP script generates output it stores it into local files. When the script is called again it check whether the file with previous output exists and if true returns the content of this file. It's mostly done with playing around the "output buffer". Somthing like this is described in this article.

使用一种操作码缓存插件,其中已编译的PHP代码存储在内存中.其中最流行的是APC,也就是eAccelerator.

Using a kind of opcode caching plugin, where the compiled PHP code is stored in memory. The most popular of this one is APC, also eAccelerator.

现在的问题是使用这两种技术还是仅使用其中一种是有意义的.我认为第一种方法的实现有点复杂且耗时,而第二种方法似乎很简单,您只需要安装模块即可.

Now the question is whether it make any sense to use both of the techniques or just use one of them. I think that the first method is a bit complicated and time consuming in the implementation, when the second one seem to be a simple one where you just need to install the module.

我在Ubuntu/Debian上使用PHP 5.3(PHP-FPM).

I use PHP 5.3 (PHP-FPM) on Ubuntu/Debian.

顺便说一句,还有什么其他方法可以缓存PHP代码或输出,我在这里没有提到?他们值得考虑吗?

BTW, are there any other methods to cache PHP code or output, which I didn't mention here? Are they worth considering?

推荐答案

您应该始终拥有APC之类的操作码缓存.它的目的是加快代码解析速度,并将在以后的版本中捆绑到PHP中.目前,这是在任何服务器上的简单安装,不需要您编写或更改任何代码.

You should always have an opcode cache like APC. Its purpose is to speed up the parsing of your code, and will be bundled into PHP in a future version. For now, it's a simple install on any server and doesn't require you write or change any code.

但是,缓存操作码对加快代码的实际执行速度没有任何作用.您的瓶颈通常是花在与数据库对话或从磁盘读取数据上的时间.缓存程序的输出可避免不必要的资源使用,并可将响应速度提高几个数量级.

However, caching opcodes doesn't do anything to speed up the actual execution of your code. Your bottlenecks are usually time spent talking to databases or reading to/from disk. Caching the output of your program avoids unnecessary resource usage and can speed up responses by orders of magnitude.

您可以在堆栈的许多不同位置以多种方式输出缓存.如您所建议,首先可以在您自己的代码中进行此操作,方法是缓冲输出,将其写入文件并在后续请求中从该文件读取.

You can do output caching many different ways at many different places along your stack. The first place you can do it is in your own code, as you suggested, by buffering output, writing it to a file, and reading from that file on subsequent requests.

尽管如此,这仍然需要在每个请求上执行您的PHP代码.您可以在Web服务器级别缓存输出,也可以跳过该步骤.制定一组mod_rewrite规则将允许Apache提供静态文件而不是PHP代码(如果存在),但是您必须手动或通过计划任务重新生成缓存的版本,因为您的PHP代码将无法在其上运行每个请求都这样做.

That still requires executing your PHP code on each request, though. You can cache output at the web server level to skip that as well. Crafting a set of mod_rewrite rules will allow Apache to serve the static files instead of the PHP code when they exist, but you'll have to regenerate the cached versions manually or with a scheduled task, since your PHP code won't be running on each request to do so.

您还可以将代理放在Web服务器的前面,并使用它来缓存输出.如今,Varnish是一种流行的选择,与在同一服务器上运行PHP脚本的Apache相比,缓存每秒可处理的请求数量要高出数百倍.缓存是在代理级别创建和配置的,因此,当缓存过期时,请求将传递到您的脚本,该脚本将按正常运行的方式生成页面的新版本.

You can also stick a proxy in front of your web server and use that to cache output. Varnish is a popular choice these days and can serve hundreds of times more request per second with caching than Apache running your PHP script on the same server. The cache is created and configured at the proxy level, so when it expires, the request passes through to your script which runs as it normally would to generate the new version of the page.

这篇关于选择一种PHP缓存技术:将输出缓存到文件中,而不是操作码缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆