APC (Alternative PHP Cache) 提供 opcode 快取與使用者資料快取。APCu 為去除 opcode 功能的分支 (PHP 5.5+ 內建 OPcache)。本文整理編譯安裝、兩套生產環境配置範本與完整參數說明。
./configure --enable-apc --enable-apc-spinlocks --disable-apc-pthreadmutex
make && make install
在 php.ini 載入:extension=apc.so (或 apcu.so)。
apc.enabled=1
apc.stat = 0 ; 不檢查檔案修改,效能最高
apc.stat_ctime = 0
apc.shm_size = 64M
apc.shm_segments = 1
apc.num_files_hint = 1000
apc.ttl = 0 ; 永不過期 (需手動清理或重啟)
apc.slam_defense = 0
apc.write_lock = 1
apc.file_update_protection = 2
部署新版本需重啟 PHP-FPM/Apache 使快取失效。
apc.enabled=1
apc.stat = 1 ; 每請求檢查 mtime
apc.stat_ctime = 1 ; 同時檢查 ctime (防 svn/rsync 導致 inode 不變)
apc.shm_size = 64M
apc.shm_segments = 1
apc.num_files_hint = 1000
apc.ttl = 86400 ; 24 小時 TTL
apc.slam_defense = 0
apc.write_lock = 1
apc.file_update_protection = 2
| 參數 | 類型 | 說明 | 建議值 |
|---|---|---|---|
| apc.enabled | bool | 啟用/停用 APC | 1 |
| apc.shm_segments | int | 共享記憶體區段數 | 1 |
| apc.shm_size | int | 每區段大小 (MB) | 64M-256M |
| apc.num_files_hint | int | 預估快取檔案數 | 1000+ |
| apc.ttl | int | 快取條目存活秒數 (0=永久) | 7200-86400 |
| apc.user_ttl | int | 使用者快取 TTL | 7200-86400 |
| apc.gc_ttl | int | 垃圾回收表條目存活秒數 | 3600 |
| apc.stat | bool | 檢查腳本更新 (關閉大幅提升效能) | 0/1 |
| apc.write_lock | bool | 寫入鎖防止並發編譯同一檔案 | 1 |
| apc.file_update_protection | int | 忽略修改時間 < N 秒的檔案 (防不完整寫入) | 2 |
| apc.enable_cli | bool | CLI 模式啟用 (僅測試用) | 0 |
| apc.max_file_size | int | 超過此大小不快取 (預設 1M) | 1M |
| apc.rfc1867 | bool | 啟用上傳進度追蹤 | 0/1 |
| apc.lazy_functions | int | 函式延遲載入 (APCu 5.1+) | 1 |
| apc.lazy_classes | int | 類別延遲載入 (APCu 5.1+) | 1 |
apc.php 管理介面觀察 Cache full count、Fragmentation、Hit rate。