配制调忧
Zend OPcache:
Nginx 把HTTP请求转发给PHP-FPM, PHP-FPM再把请求交给某个PHP子进程处理。PHP进程找到相应的PHP脚本后,读取脚本,把PHP脚本编译成操作码(或字节码)格式,然后执行编译得到的操作码,生成响应。最后把HTTP响应发送给nginx,nginx再把响应发送给HTTP客户端。每次HTTP请求都要消耗很多资源。
我们可以缓存编译每个PHP脚本得到的操作码,加速这个处理过程。缓存后,我们可以从缓存中直接读取并执行预先编译好的操作码,不用每次处理HTTP请求时都查找,读取和编译PHP脚本。
Object Caching:
There are times when it can be beneficial to cache individual objects in the code, such as with data that is expensive to get or database calls where the result is unlikely to change. Use object caching software to hold these pieces of data in memory for extremely fast access later on. If save these items to a data store after you retrieve them, then pull them directly from the cache for following requests, you can gain a significant improvement in performance as well as reduce the load on your database servers.
The most commonly used memory object caching systems are APCu and memcached. APCu is an excellent choice for object caching, it includes a simple API for adding your own data to its memory cache and is very easy to setup and use. The one real limitation of APCu is that it is tied to the server it's installed on. Memcached on the other hand is installed as a separate service and can be accessed across the network, meaning that you can store objects in a hyper-fast data store in a central location and many different systems can pull from it.
Note that when running PHP as a (Fast-)CGI application inside your webserver, every PHP process will have its own cache, i.e. APCu data is not shared between your worker processes, as it's not tied to the PHP processes.
In a networked configuration APCu will usually outperform memcached in terms of access speed, but memcached will be able to scale up faster and further. If you do not expect to have multiple servers running your application, or do not need the extra features that memcached offers then APCu is probably your best choice for object caching.
Example logic using APCu:
<?php // check if there is data saved as 'expensive_data' in cache $data = apc_fetch('expensive_data'); if ($data === false) { // data is not in cache; save result of expensive call for later use apc_add('expensive_data', $data = get_expensive_data()); } print_r($data);
APCu used in php-fpm or cli could be tricky because every threads is individual!
真实路径缓存:
PHP会缓存应用使用的文件路径,这样,每次包含或导入文件时就无需不断搜索包含路径了。这个缓存叫真实路径缓存(Realpath Cache)。如果运行很大的PHP文件如Drupal,使用了大量文件,增加PHP真实路径缓存的大小能得到更好的性能。