当前位置: 首页 > 工具软件 > aredis > 使用案例 >

【thinkphp5操作redis系列教程】connect(或open)连接

公羊光明
2023-12-01

1.连接函数

    /**
     * Connects to a Redis instance.
     *
     * @param string    $host       can be a host, or the path to a unix domain socket
     * @param int       $port       optional
     * @param float     $timeout    value in seconds (optional, default is 0.0 meaning unlimited)
     * @param null      $reserved   should be null if $retry_interval is specified
     * @param int       $retry_interval  retry interval in milliseconds.
     * @return bool                 TRUE on success, FALSE on error.
     */
    public function connect( $host, $port = 6379, $timeout = 0.0, $reserved = null, $retry_interval = 0 ) {}

 

2.默认端口连接

<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        $con = $redis->connect('127.0.0.1');
        var_dump($con);//bool(true)

    }

}

3.端口连接(默认端口6379)

<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        $con = $redis->connect('127.0.0.1',6379);
        var_dump($con);//bool(true)

    }

}

4.超时放弃连接

<?php
namespace app\index\controller;
use Redis;
class Index
{
    public function index()
    {
        $redis = new Redis();
        //默认为0,即连接没有限制,此处为短连接,超过3秒放弃连接
        $con = $redis->connect('127.0.0.1',6379,3);
        var_dump($con);//bool(true)

    }

}

备注:open()用法同connect()

 类似资料: