标签:exists namespace str connect min turn 放弃 class orm
//1.config目录下新建redis.php
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/12/18
* Time: 13:14
*/
return [
'host' => '127.0.0.1',
'port' => '6379'
];
//2.extend目录下新建->module文件夹->Redis.php文件
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/12/18
* Time: 13:16
*/
namespace Module;
class Redis extends \Redis
{
public static function redis() {
$con = new \Redis();
$con->connect(config('redis.host'), config('redis.port'), 5); //连接redis 超过5秒放弃
$con->auth('123456'); //密码验证 没有可不写
$con->select(0); //选择数据库
return $con;
}
}
//3.设置助手函数 thinkphp文件下->helper.php文件
if (!function_exists('redis')) {
/**
* 获取容器对象实例
* @return Container
*/
function redis()
{
return \module\Redis::redis();
}
}
//4.控制器中使用
redis()->set('test','111');
$data = redis()->get('test'); //111
标签:exists namespace str connect min turn 放弃 class orm
原文地址:https://www.cnblogs.com/L-ran/p/12059570.html