在 CentOS + Nginx + PHP 5.4+ 環境下,完整建置 Laravel 5.0 專案並整合 MySQL(主儲存)與 Redis(快取層),示範 Controller → Model → Repository 模式實現快取優先讀取策略。
php-mysql php-mcrypt php-mbstring php-tokenizer php-openssltry_files $uri $uri/ /index.php?$query_string;storage/、vendor/ 權限 chmod -R 766demo 資料庫與 tbl_item 表。config/database.php 設定 MySQL 連線。App\Item 繼承 Eloquent,fetchAll() 回傳 JSON。ItemController@showItem 呼叫 Model 並 Log::info 記錄來源。Route::get('/item/{id}', 'ItemController@showItem')。wget && make && src/redis-server。predis/predis:~1.0(無需 PHP 擴展)。config/database.php 新增 redis 陣列配置 host=127.0.0.1, port=6379。$redis = Redis::connection('default');
$cache = $redis->get('userList');
if ($cache) {
$users = $cache; Log::info('from redis');
} else {
$users = $this->model->fetchAll();
$redis->set('userList', $users);
Log::info('from mysql');
}
composer update 記憶體不足 → 重啟伺服器或調整 memory_limit。bind 127.0.0.1、密碼認證。del('userList') 或設定 TTL 自動過期。此架構讀請求優先命中 Redis,寫請求更新 MySQL 並清除快取,經典的 Cache-Aside 模式,顯著降低資料庫壓力。