Windows 環境部署 Memcached 服務端並啟用 PHP memcache 擴展的完整步驟記錄,含 VC9 版本選擇、服務安裝參數、php.ini 完整配置範本。
PHP Application
│
▼ (memcache extension)
Memcached Client Library (php_memcache.dll)
│
▼ (TCP 11211)
Memcached Server (memcached.exe Windows 服務)
官方釋出頁:GitHub Wiki 或 https://www.memcached.org/files/memcached-1.4.4-win32.zip
C:\webserver\memcached\
├── memcached.exe
└── (無其他依賴檔,VC6 編譯自包含)
C:\webserver\memcached> memcached -d install
常見錯誤:缺少 MSVCP71.dll → 下載放入 C:\Windows\System32\ 或 Memcached 目錄。
memcached -d start
驗證:
wmic process get description, executablepath | findstr memcached.exe
# 輸出: memcached.exe C:\webserver\memcached\memcached.exe
php -v
# 輸出範例: PHP 5.3.28 (cli) (built: Dec 11 2013) ...
# 關鍵資訊: VC9, TS (Thread Safe) 或 NTS (Non-Thread Safe)
| PHP 版本 | 下載連結 |
|---|---|
| 5.3 VC9 TS (Apache 模組) | Thread Safe 版 |
| 5.3 VC9 NTS (FastCGI/IIS) | Non-Thread Safe 版 |
關鍵:必須匹配 VC 版本(VC9/VC11/VC14)與執行緒模式(TS/NTS),否則啟動報錯 Unable to load dynamic library。
# 解壓取得 php_memcache.dll
copy php_memcache.dll C:\webserver\php5.3\nts\ext\
# 編輯 php.ini,末尾新增
[memcache]
extension=php_memcache.dll
; 連線池與效能調優
memcache.allow_failover = 0
memcache.max_failover_attempts = 20
memcache.chunk_size = 8192
memcache.default_port = 11211
memcache.hash_strategy = "consistent" ; 一致性哈希,支援動態增減節點
memcache.hash_function = "crc32" ; 或 fnv1a
iisreset /restart
# 或 Apache
httpd -k restart
php -m | findstr memcache
# 或建立 phpinfo.php 檢查 memcache 區塊
[memcache]
extension=php_memcache.dll
memcache.allow_failover = 0
memcache.max_failover_attempts = 20
memcache.chunk_size = 8192
memcache.default_port = 11211
memcache.hash_strategy = "consistent"
memcache.hash_function = "crc32"
; Session 存儲至 Memcached(可選)
; session.save_handler = "memcache"
; session.save_path = "tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
<?php
$mem = new Memcache();
$mem->connect('127.0.0.1', 11211) or die('連線失敗');
$mem->set('test_key', 'Hello Memcached ' . date('H:i:s'), 0, 60);
echo $mem->get('test_key');
$mem->close();
?>
session.save_path 一致Windows 開發環境部署 Memcached 只需 10 分鐘,是本地快取開發測試的最佳選擇。