1.首先你的php得是已經安裝了redis擴展的
2.在tp里找到config.php配置文件,找到cache,改成下面的樣子
'cache' => [ // 選擇模式 'type' => 'complex', // 默認(文件緩存) 'default'=>[ // 驅動方式 'type' => 'File', // 緩存保存目錄 'path' => CACHE_PATH, // 緩存前綴 'prefix' => '', // 緩存有效期 0表示永久緩存 'expire' => 0, ], //redis緩存設置 'redis' => [ // 驅動方式 'type' => 'redis', // 服務器地址 'host' => '127.0.0.1', //redis服務器ip 'password' => '', 'port' => '6379', 'password'=> "", 'timeout' => 3600 ], ],
3.控制器里寫上以下代碼測試一下
<?php namespace app\index\controller; class Index { public $redis; public function __construct(){ $this->redis = new \Redis(); $this->redis->connect('127.0.0.1',6379); } public function index() { $redis = $this->redis; echo $redis->ping(); } }