上路抗压,野爹常来

TP5连接redis

config配置

    /*redis链接*/
    'redis' => [
        'default' => [
            'type' => 'redis',
            'debug' => true,
            'hostname' => '127.0.0.1',
            'port' => 6379,
            'auth' => '',
            'timeout' => 3,//单位秒
            'pconnect' => 0,
            'autoconnect' => 0
        ]
    ],

使用

$arrConfig = Config::get('redis.default');
$objRedis = new \Redis();
if(!$arrConfig['pconnect'])
    $objRedis->connect($arrConfig['hostname'], $arrConfig['port'],$arrConfig['timeout']);
else
    $objRedis->pconnect($arrConfig['hostname'], $arrConfig['port'],$arrConfig['timeout']);
$objRedis->auth($arrConfig['auth']);
if(!$objRedis) parent::returnCode(1999,'数据库连接失败!');
return $objRedis;

##config.php
/*redis链接*/
    'redis' => [
        'default' => [
            'type' => 'redis',
            'debug' => true,
            'hostname' => '127.0.0.1',
            'port' => 6379,
            'auth' => '',
            'timeout' => 3,//单位秒
            'pconnect' => 0,
            'autoconnect' => 0
        ]
    ],
posted @ 2021-05-20 11:11  上路抗压  阅读(524)  评论(0编辑  收藏  举报