【PHP、Redis、laravel 】laravel 5.3 使用 redis 作為 session 儲存體

動機


接續前篇 【AWS、ElastiCache】redis 簡單設定驗證
現在來設定 laravel 5.3 去接redis 並把 session存進去

動手做


首先先改 .env 環境變數檔
REDIS_HOST=giver-lab-redis.ym07ou.cache.amazonaws.com
REDIS_PASSWORD=null
REDIS_PORT=6379
接著設定 app\config\database.php
'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            //'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],
然後再來設定app\config\session.php
把這行
//'driver' => env('SESSION_DRIVER', 'file'),
改成
'driver' => env('SESSION_DRIVER', 'redis'),
設定完成,有 framework 真好 XD

驗證


先打開console,然後下指令monitor
[root@ip-10-128-110-42 opt]# telnet giver-lab-redis.apne1.cache.amazonaws.com 6379
Trying 10.128.120.143...
Connected to giver-lab-redis.apne1.cache.amazonaws.com.
Escape character is '^]'.
monitor
+OK
然後隨便去觸動一下程式
monitor
+OK
+1484027435.143364 [0 10.128.110.42:54278] "select" "0"
+1484027435.146274 [0 10.128.110.42:54278] "get" "laravel:vtayPQv0aCheOzcruGzVz4svooUWss7FS2QVwIQD"
+1484027435.155385 [0 10.128.110.42:54278] "setex" "laravel:vtayPQv0aCheOzcruGzVz4svooUWss7FS2QVwIQD" "7200" "s:306:\"a:5:{s:6:\"_token\";s:40:\"oSMsyaOfmbPzqBzyVfwXAqROe4eNR19T0LYCXtAX\";s:9:\"_previous\";a:1:{s:3:\"url\";s:44:\"http://giveradmlab.104executive.com.tw/stage\";}s:6:\"_flash\";a:2:{s:3:\"old\";a:0:{}s:3:\"new\";a:0:{}}s:6:\"userid\";s:4:\"2159\";s:9:\"_sf2_meta\";a:3:{s:1:\"u\";i:1484027435;s:1:\"c\";i:1484020165;s:1:\"l\";s:1:\"0\";}}\";"
+1484027435.790210 [0 10.128.110.42:54280] "select" "0"
+1484027435.793114 [0 10.128.110.42:54280] "get" "laravel:vtayPQv0aCheOzcruGzVz4svooUWss7FS2QVwIQD"
+1484027435.966698 [0 10.128.110.42:54280] "setex" "laravel:vtayPQv0aCheOzcruGzVz4svooUWss7FS2QVwIQD" "7200" "s:306:\"a:5:{s:6:\"_token\";s:40:\"oSMsyaOfmbPzqBzyVfwXAqROe4eNR19T0LYCXtAX\";s:9:\"_previous\";a:1:{s:3:\"url\";s:44:\"http://giveradmlab.104executive.com.tw/stage\";}s:6:\"_flash\";a:2:{s:3:\"old\";a:0:{}s:3:\"new\";a:0:{}}s:6:\"userid\";s:4:\"2159\";s:9:\"_sf2_meta\";a:3:{s:1:\"u\";i:1484027435;s:1:\"c\";i:1484020165;s:1:\"l\";s:1:\"0\";}}\";"
驗證成功!收工。

留言