方法1:
Controller
<?php namespace app\index\controller; use think\Controller; use think\session\driver\Redis; class Index extends Controller { public function index() { $redis = new Redis(); if(!$redis->has('str')){ var_dump($redis->set('str','this is redis_str')); }else{ var_dump($redis->get('str')); } } }
方法2:(前方雷區!!!)
config.php
// +----------------------------------------------------------------------
// | 緩存設置
// +----------------------------------------------------------------------
'cache' => [
// 驅動方式
'type' => 'File',
// 緩存保存目錄
'path' => CACHE_PATH,
// 緩存前綴
'prefix' => '',
//緩存有效期 0表示永久緩存
'expire' => 0,
],
'redis' => [
// 驅動方式
'type' => 'redis',
// 服務器地址
'host' => '127.0.0.1', //redis服務器ip
'port' => '6379',
'password'=> "",
'timeout' => 86400
],
Controller
<?php namespace app\index\controller; use think\Cache; use think\Controller; class Index extends Controller { public function index() { if(!Cache::has('str')){ var_dump(Cache::set('str','this is redis_str')); }else{ var_dump(Cache::get('str')); } } }