Zend Opcache(前身 Optimizer+)是 Zend 開源的 opcode 快取擴展,將預編譯的腳本存放於共享記憶體,避免重複編譯,大幅降低 CPU 與記憶體開銷,是 PHP 5.5+ 內建的效能利器。
PHP 腳本執行流程:原始碼 → 詞法分析 → 編譯為 Opcode(中間碼) → 執行。Opcode 快取保存編譯後的中間碼,下次請求直接從記憶體讀取執行,跳過編譯階段。現代快取器使用共享記憶體,無需反序列化,效能提升顯著。
適用場景: CPU/記憶體為瓶頸的計算密集型應用;若瓶頸在 I/O(如資料庫查詢),提升有限但仍有助降低資源消耗。
趨勢:Opcache 已取代 APC 成為標準選擇。
git clone https://github.com/zendtech/ZendOptimizerPlus.git
cd ZendOptimizerPlus
phpize
./configure --with-php-config=/usr/bin/php-config
make && make install
需安裝 php-devel,確認 phpize 路徑。
yum install php-pecl-zendopcache
# 或搭配 Remi 升級 PHP 5.4+
yum --enablerepo=remi install php-pecl-zendopcache
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
php -v
# 輸出應包含:with Zend OPcache v7.x.x
注意: Opcache 與 eAccelerator 衝突,安裝前請先移除舊加速器。