问题描述
进行了一些研究之后,我努力寻找关于如何以及是否缓存PHP include()
文件的明确答案.
After doing some research i'm struggling to find a definitive answer on how and if a PHP include()
file is cached.
我找到的最接近的是此处但这对我来说意义不大.
The closest i've found is here but it doesn't quite make sense to me.
我有几种方法可以使用不同文件中的其他方法,并且我想避免将include()放在文件的顶部,但是我不确定这会如何影响性能.
I have several methods that use other methods from different files and I want to avoid placing include()'s just at the top of the file but i'm not sure how this might affect performance.
感谢您的帮助.
推荐答案
PHP是一种解释型语言.默认的PHP运行时将PHP源代码编译为称为PHP字节码的中间表示形式,然后将其执行.字节码缓存将PHP源代码的编译后的表示形式存储在共享内存中.这样就无需在每个请求上加载和编译源代码,从而显着提高了性能(每秒最多增加70%的请求).
PHP is an interpreted language. The default PHP runtime compiles PHP sourcecode to an intermediate representation called PHP bytecode which is then executed. A bytecode cache stores this compiled representation of PHP sourcecode in shared memory. This eliminates the need to load and compile sourcecode on each request which leads to a significant increase in performance (up to 70% more requests per second).
执行PHP脚本时的基本思路分两个步骤:
The basic idea, when executing a PHP script is in two steps:
- 首先:用纯文本格式编写的PHP代码编译为操作码
- 然后:执行这些操作码.当您拥有一个PHP脚本时,只要不对其进行修改,操作码将始终相同;因此,每次执行脚本时都要进行编译阶段,这是在浪费CPU时间.
- First: the PHP code, written in plain-text, is compiled to opcodes
- Then: those opcodes are executed.When you have one PHP script, as long as it is not modified, the opcodes will always be the same ; so, doing the compilation phase each time that script is to be executed is kind of a waste of CPU-time.
为防止这种冗余编译,可以使用一些操作码缓存机制.
To prevent that redundant-compilation, there are some opcode caching mechanism that you can use.
将PHP脚本编译为操作码后,这些将保存在RAM中-下次执行该脚本时可直接从内存中使用这些操作码;阻止编译一次又一次地完成.
Once the PHP script has been compiled to opcodes, those will be kept in RAM -- and directly used from memory the next time the script is to be executed ; preventing the compilation from being done again and again.
了解详情
- https://blog.graphiq.com/500x-faster-caching-than-redis-memcache-apc-in-php-hhvm-dcd26e8447ad#.tsokdw9d4
- https://github.com/TerryE/opcache/wiki/The-Zend-Engine-and-opcode-caching#opcode-caching-with-opcache
- https://juokaz.com/blog/from-php-to-机器代码
这篇关于是否缓存了本地PHP include()文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!