参数参考位置:thinkphp\library\think\cache\driver
class Redis extends Driver { protected $options = [ 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', ];
方式一:控制器
public function index(){ $config = [ 'host' => '192.168.70.161', 'port' => 6379, 'password' => 'admin999', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => '', ]; $Redis=new Redis($config); $Redis->set("test","test"); echo $Redis->get("test"); }
方式二:符合类型缓存(配置文件)
config.php
'cache' => [ // 使用复合缓存类型 'type' => 'complex', // 默认使用的缓存 'default' => [ // 驱动方式 'type' => 'redis', // 缓存保存目录 'path' => CACHE_PATH, // 文件缓存 ], 'file' => [ // 驱动方式 'type' => 'file', // 设置不同的缓存保存目录 'path' => CACHE_PATH, ], // // 驱动方式 // redis缓存 'redis' => [ // 驱动方式 'type' => 'redis', // 服务器地址 'host' => '192.168.70.161', 'expire' => 0, 'port' => 6379, 'password'=>'hello', 'select'=>14 //选择几号库 ],
Cache::store('redis')->set('name','999'); Cache::store('file')->set('name1','999');
链接方式三:配置文件
//redis连接 $redis = new \Redis(); $redis->connect('192.168.70.161','6379'); $redis->auth('hello'); $redis->select(14); $redis->Lrange('runoobkey','0','10')
// thinkphp 默认不能使用redis的push、lLen等操作,需要连接自带的redis.php
方式一和方式二连接都无法使用redis自带的push,lLen,Lrange方法,只能使用thinkphp自带的redis几个方法