APC(Alternative PHP Cache) 是 PHP 的 opcode 緩存擴展,透過將編譯後的中間代碼(opcode)存放在共享記憶體中,避免重複編譯 PHP 腳本,大幅提升執行效能。APC 緩存分為兩大類:
apc_store()、apc_fetch() 等 API 手動存取的鍵值資料,適用於配置、計數器、計算結果等熱門資料。apc_cache_make_user_key(),對開發者提供的字串鍵使用 PHP 內核 hash 函數(zend_inline_hash_func)生成 h 值,決定槽位。apc_cache_make_file_key()。
apc.stat=On:h = device + inode,檔案更新自動重新編譯。apc.stat=Off:h = 絕對路徑 hash,修改檔案需重啟 Web 服務生效,效能最佳。apc_store() → _apc_store() → apc_cache_make_user_entry()slot = cache->slots[key.h % cache->num_slots]php_apc.dll(PECL 官網)ext/ 目錄php.ini:
extension=php_apc.dll
apc.rfc1867 = on ; 啟用上傳進度追蹤
apc.max_file_size = 100M ; 最大緩存檔案
upload_max_filesize = 100M
post_max_size = 100M
phpinfo() 驗證wget https://pecl.php.net/get/APC-3.1.13.tgz
tar -zxvf APC-3.1.13.tgz
cd APC-3.1.13
/usr/local/php/bin/phpize
./configure --enable-apc --enable-mmap --enable-apc-spinlocks \
--disable-apc-pthreadmutex \
--with-php-config=/usr/local/php/bin/php-config
make && sudo make install
修改 /usr/local/php/etc/php.ini 加入配置段(見下節),重啟 PHP-FPM/Apache。
| 參數 | 類型 | 預設 | 說明與調優建議 |
|---|---|---|---|
| apc.enabled | bool | 1 | 總開關。靜態編譯時唯一禁用途徑 |
| apc.shm_segments | int | 1 | 共享記憶體分段數。單段受限於系統上限時可增加 |
| apc.shm_size | int | 32M | 關鍵參數。每段大小(MB)。根據專案檔案數與用戶資料量調整,建議 128M~512M |
| apc.optimization | int | 0 | 優化等級(實驗性),0 關閉,>0 啟用,生產環境建議 0 |
| apc.num_files_hint | int | 1000 | 預估不同源檔案數。大型站點(成千檔案)需調大,避免重新 hash |
| apc.ttl | int | 0 | 關鍵。緩存條目最大存活秒數。0=永不自動過期(需手動清理或重啟)。生產建議 7200~86400 或 0 配合部署流程清理 |
| apc.gc_ttl | int | 3600 | 垃圾回收列表存活秒數。防止請求中斷導致舊版本 opcode 殘留 |
| apc.cache_by_default | bool | On | On=緩存所有檔案。Off 搭配 filters 白名單模式 |
| apc.filters | string | POSIX 正則清單。+ 開頭=白名單,- 開頭=黑名單。匹配 include/require 參數而非絕對路徑 | |
| apc.mmap_file_mask | string | MMAP 檔案遮罩。/tmp/apc.XXXXXX(檔案支援)、/apc.shm.XXXXXX(POSIX shm)、/dev/zero(匿名映射,推薦) | |
| apc.slam_defense | int | 1 | 防群體衝突機率(%)。高併發時設 75~90 降低同時重建 opcode 機率。0 關閉 |
| apc.file_update_protection | int | 2 | 檔案修改後延遲緩存秒數。防止讀到寫入中的檔案。原子部署(mv)可設 0 |
| apc.enable_cli | int | 0 | CLI 模式啟用 APC。僅供測試/除錯,生產 CLI 腳本建議關閉 |
| apc.stat | bool | On | 效能關鍵。On=每請求 stat 檢查 mtime。Off=極致效能,部署需重啟服務 |
| apc.include_once_override | bool | Off | 優化 include_once/require_once。實驗性,慎用 |
| apc.lazy_functions / apc.lazy_classes | int | 0 | 延遲載入函數/類別定義。大型框架專案可嘗試 1 |
--enable-apc-spinlocks 使用自旋鎖,高併發下效能優於 pthread mutex。apc.php(解壓套件內附),務必修改預設管理員帳密。apc.php 統計 → 調整 shm_size、num_files_hint、user_entries_hint → 重啟驗證 → 重複直到命中率 > 95%、碎片率 < 10%。apc.stat=0:部署流程需包含「重啟 PHP-FPM/Apache」步驟,換取最大效能。apc_define_constants() 緩存大量常數定義,優於 define()。但 PECL hidef 效能更佳。<?php
/**
* APC 緩存操作封裝
* 適用於 PHP 5.4 / APC 3.1.x 環境
*/
class ApcCache {
/** 寫入緩存 */
public function set($key, $value, $ttl = 0) {
// $ttl=0 表示永久(受 apc.ttl 限制)
return apc_store($key, $value, $ttl ?: null);
}
/** 讀取緩存 */
public function get($key) {
return apc_fetch($key);
}
/** 刪除單個鍵 */
public function delete($key) {
return apc_delete($key);
}
/** 清空用戶緩存(謹慎使用) */
public function clearAll() {
apc_clear_cache('user');
return apc_clear_cache(); // 同時清除系統緩存 opcode
}
/** 檢查鍵是否存在 */
public function exists($key) {
return apc_exists($key);
}
/** 原子增長(計數器) */
public function increment($key, $step = 1) {
return apc_inc($key, (int)$step);
}
/** 原子減少 */
public function decrement($key, $step = 1) {
return apc_dec($key, (int)$step);
}
/** 獲取緩存統計資訊 */
public function getStats() {
return apc_cache_info();
}
}
// 使用範例
$cache = new ApcCache();
$cache->set('app_config', ['debug' => false, 'version' => '1.2.3'], 3600);
$config = $cache->get('app_config');
$cache->increment('page_views');
| APC 功能 | 現代替代方案 (PHP 5.5+) |
|---|---|
| Opcode 緩存 | OPcache(內建,opcache.enable=1) |
| 用戶資料緩存 | Redis(推薦,功能豐富)或 Memcached(簡單、多執行緒) |
| 上傳進度追蹤 | Session 上傳進度(session.upload_progress.enabled=1)或前端 AJAX 輪詢 |
| apc.php 監控 | Redis INFO / Memcached stats / 專用監控系統(Prometheus+Grafana、Datadog 等) |